在
Swift中,String结构也被视为类对象,就像使用NSCoder encodeObject(_:forKey :)方法一样.我知道String直接与objective-c类NSString桥接,但有没有办法制作一个行为相似的自定义结构?也许将它连接到一个自定义类?我希望能够做到这样的事情:
原文链接:https://www.f2er.com/swift/319386.htmlstruct 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") } }