struct MyRect : CGRect {...}
Is it possible at all in swift to subclass a Structure?
I have already found out on Apple official website an example: This example extends the CGRect structure to contain a computed area property:
extension CGRect {
var area: CGFloat {
return width * height
}
}
let rect = CGRect(x: 0.0, y: 0.0, width: 10.0, height: 50.0)
let area = rect.area
How can i define a subclass of a structure ?
* Structs have statically-dispatched methods and properties; there's no ability to override.
Use Classes When You Need to Control Identity Classes in Swift come with a built-in notion of identity because they're reference types. This means that when two different class instances have the same value for each of their stored properties, they're still considered to be different by the identity operator ( === ).
The syntax for defining classes and structs in Swift are also similar. To define a class or struct in Swift, use the keyword class or struct followed by the name of the class or struct with curly braces. As a note, ensure that classes and structs' names in Swift follow the PascalCase naming convention.
In Swift, a struct is used to store variables of different data types. For example, Suppose we want to store the name and age of a person. We can create two variables: name and age and store value.
It is not possible to subclass a struct
in Swift, only classes can be subclassed. An extension
is not a subclass, it's just adding additional functionality on to the existing struct
, this is comparable to a category in Objective-C.
Please see the Apple Swift documentation here to read about the differences between struct
and class
.
You cannot subclass structures in Swift but you can, in some sense, mimic subclassing by creating a structure that simply vends out the structure that you wish to subclass. For example, if you wanted to subclass Calendar
(a structure), you could create a structure that returns configured calendars.
struct WorldCalendar {
let american1: Calendar = {
var c = Calendar(identifier: .gregorian)
c.timeZone = .current
c.locale = Locale(identifier: "en_US_POSIX")
return c
}()
static let american2: Calendar = {
var c = Calendar(identifier: .gregorian)
c.timeZone = .current
c.locale = Locale(identifier: "en_US_POSIX")
return c
}()
static func american3() -> Calendar {
var c = Calendar(identifier: .gregorian)
c.timeZone = .current
c.locale = Locale(identifier: "en_US_POSIX")
return c
}
}
let worldCalendar = WorldCalendar()
let cal1 = worldCalendar.american1
let cal2 = WorldCalendar.american2
let cal3 = WorldCalendar.american3()
How you vend the configured structures (as an instance property, a static property, a static function, etc.) is just a matter of what better fits into your application and, frankly, personal preference.
Since Swift 5.1 it is now possible to do something similar to what you asked for: using composition, with KeyPath
and dynamicMemberLookup
. Have a look: https://medium.com/@acecilia/struct-composition-using-keypath-and-dynamicmemberlookup-kind-of-struct-subclassing-but-better-6ce561768612?source=friends_link&sk=da479578032c0b46c1c69111dfb6054e
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With