Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I subclass a struct in Swift?

Tags:

xcode

ios

swift

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 ?

like image 854
J A S K I E R Avatar asked Nov 25 '15 11:11

J A S K I E R


People also ask

Can we override the structure in Swift?

* Structs have statically-dispatched methods and properties; there's no ability to override.

When should you use classes over structs Swift?

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 ( === ).

How do you create a struct class in Swift?

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.

How do you define a struct in Swift?

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.


3 Answers

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.

like image 98
Sam Clewlow Avatar answered Oct 22 '22 06:10

Sam Clewlow


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.

like image 6
liquid LFG UKRAINE Avatar answered Oct 22 '22 06:10

liquid LFG UKRAINE


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

like image 2
acecilia Avatar answered Oct 22 '22 06:10

acecilia