In Swift a String
structure is also treated as a class object like when using the NSCoder
encodeObject(_:forKey:)
method. I do know that String
is directly bridged with the objective-c class, NSString
, but is there a way to make a custom struct
that behaves similarly? Perhaps bridge it to a custom class? I want to be able to do something like this:
struct SortedArray <Value: Comparable> {}
// Would I need to create a bridge between
// SortedArray and NSSortedArray? Can I even do that?
class NSSortedArray <Value: Comparable> : NSObject, NSCoding {
required init?(coder aDecoder: NSCoder) {}
func encodeWithCoder(aCoder: NSCoder) {}
}
class MyClass : NSObject, NSCoding {
private var objects: SortedArray<String> = SortedArray<String>()
required init?(coder aDecoder: NSCoder) {
guard let objects = aDecoder.decodeObjectForKey("objects") as? SortedArray<String> else { return nil }
self.objects = objects
}
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(objects, forKey: "objects")
}
}
In Swift, structs are value types whereas classes are reference types. When you copy a struct, you end up with two unique copies of the data. When you copy a class, you end up with two references to one instance of the data. It's a crucial difference, and it affects your choice between classes or structs.
A struct definition is just a blueprint. To use a struct, we need to create an instance of it. For example, struct Person { var name = " " var age = 0 } // create instance of struct var person1 = Person() Here, we have created an instance by writing the name of the structure Person followed by a default initializer ()
When choosing between structs and classes, it's important to remember the key differences: Classes are reference types, and structs are value types. If class inheritance is not needed, structs are faster and more memory efficient. Use structs for unique copies of an object with independent states.
It's because classes are reference types, only the memory location is immutable, but the content mutability is independent from it. Hence that makes Struct immutability more guaranteed than Class.
This isn't currently possible. SE-0058 will address it, but is deferred out of Swift 3. A final implementation of SE-0058 would be hoped to handle more than just ObjC bridging; for example allowing C++ or .NET bridging as well in a more generic solution.
Ultimately, the bridging between String
and NSString
is quite simple.
NSString
only has 2 instance variables (The string pointer nxcsptr
, and the length nxcslen
). String
uses _StringCore
, which only has 3 properties (_baseAddress
, _countAndFlags
, and _owner
). The conversion back and forth is hard coded, and called explicitly by the compiler. There's no automatic system implemented for generating classes out of structs, or vice versa.
You'll have to implement a struct
/class
pair (like with String
and NSString
), and implement initializers that construct one from the other.
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