I'm having trouble finding the correct way to conform the the NSSecureCoding
protocol in Swift, specifically when decoding objects that is an array of other objects.
我无法找到在Swift中符合NSSecureCoding协议的正确方法,特别是在解码作为其他对象数组的对象时。
I can't create an NSSet
of class types in swift.
我无法在swift中创建类类型的NSSet。
In Objective-C I would use
在Objective-C中我会使用
self.books = [aDecoder decodeObjectOfClasses:[NSSet setWithObjects:[NSArray class], [Book class], nil] forKey:@"books"];
in Swift I'm having issues creating the NSSet
like this :
在Swift中,我遇到了像这样创建NSSet的问题:
self.books = aDecoder.decodeObjectOfClasses(NSSet().setByAddingObject(NSArray.self).setByAddingObject(Book.self), forKey:"books")
Here's the error:
这是错误:
Type 'NSArray.Type' does not conform to protocol 'AnyObject'
3 个解决方案
#1
1
For simple objects:
对于简单对象:
self.myObject = aDecoder.decodeObjectOfClass(MyObject.self, forKey: "myObject")!
Since Swift 3.0:
自Swift 3.0起:
self.myObject = aDecoder.decodeObject(of:MyObject.self, forKey: "myObject")!
For nested objects:
对于嵌套对象:
NSSet(objects: [NSArray.self, Book.self])
#2
0
Not exactly the same as when I originally posted the question, but I've achieved what I wanted by using typed collections as seen below
与我最初发布问题时的情况不完全相同,但我已经通过使用类型集合实现了我想要的效果,如下所示
class Bookshelf: NSObject, NSSecureCoding {
var authors = [Author]()
var books = [Book]()
override init () {
super.init()
}
@objc func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(self.authors, forKey: "authors")
aCoder.encodeObject(self.books, forKey: "books")
}
@objc required init?(coder aDecoder: NSCoder) {
self.authors = (aDecoder.decodeObjectForKey("authors") as? [Author])!
self.books = (aDecoder.decodeObjectForKey("books") as? [Book])!
}
@objc static func supportsSecureCoding() -> Bool {
return true
}
}
#3
0
宣言
SWIFT
迅速
func decodeObjectOfClasses(_ classes: NSSet,
forKey key: String) -> AnyObject?
Since Swift 3.0:
自Swift 3.0起:
func decodeObject<DecodedObjectType where DecodedObjectType : NSCoding, DecodedObjectType : NSObject>(of cls: DecodedObjectType.Type, forKey key: String) -> DecodedObjectType?
OBJECTIVE-C
Objective-C的
- (id)decodeObjectOfClasses:(NSSet *)classes
forKey:(NSString *)key
let books: AnyObject? = aDecoder.decodeObjectOfClasses(NSSet().setByAddingObject(NSArray.self).setByAddingObject(Book.self), forKey:"books")
#1
1
For simple objects:
对于简单对象:
self.myObject = aDecoder.decodeObjectOfClass(MyObject.self, forKey: "myObject")!
Since Swift 3.0:
自Swift 3.0起:
self.myObject = aDecoder.decodeObject(of:MyObject.self, forKey: "myObject")!
For nested objects:
对于嵌套对象:
NSSet(objects: [NSArray.self, Book.self])
#2
0
Not exactly the same as when I originally posted the question, but I've achieved what I wanted by using typed collections as seen below
与我最初发布问题时的情况不完全相同,但我已经通过使用类型集合实现了我想要的效果,如下所示
class Bookshelf: NSObject, NSSecureCoding {
var authors = [Author]()
var books = [Book]()
override init () {
super.init()
}
@objc func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(self.authors, forKey: "authors")
aCoder.encodeObject(self.books, forKey: "books")
}
@objc required init?(coder aDecoder: NSCoder) {
self.authors = (aDecoder.decodeObjectForKey("authors") as? [Author])!
self.books = (aDecoder.decodeObjectForKey("books") as? [Book])!
}
@objc static func supportsSecureCoding() -> Bool {
return true
}
}
#3
0
宣言
SWIFT
迅速
func decodeObjectOfClasses(_ classes: NSSet,
forKey key: String) -> AnyObject?
Since Swift 3.0:
自Swift 3.0起:
func decodeObject<DecodedObjectType where DecodedObjectType : NSCoding, DecodedObjectType : NSObject>(of cls: DecodedObjectType.Type, forKey key: String) -> DecodedObjectType?
OBJECTIVE-C
Objective-C的
- (id)decodeObjectOfClasses:(NSSet *)classes
forKey:(NSString *)key
let books: AnyObject? = aDecoder.decodeObjectOfClasses(NSSet().setByAddingObject(NSArray.self).setByAddingObject(Book.self), forKey:"books")