Define struct that is treated like a class in Swift

后端 未结 3 861
-上瘾入骨i
-上瘾入骨i 2021-01-12 05:30

In Swift a String structure is also treated as a class object like when using the NSCoder encodeObject(_:forKey:) method. I do know th

3条回答
  •  北荒
    北荒 (楼主)
    2021-01-12 06:12

    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.

提交回复
热议问题