我有以下代码:
import Foundation
import Photos
class PostItem: NSObject, NSCoding {
var postDate: String?
var postTitle: String?
var postText: String?
var postImages: [PHAsset]?
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(postDate, forKey: "postDate")
aCoder.encodeObject(postTitle, forKey: "postTitle")
aCoder.encodeObject(postText, forKey: "postText")
aCoder.encodeObject(postImages, forKey: "postImages")
}
required init?(coder aDecoder: NSCoder) {
postDate = aDecoder.decodeObjectForKey("postDate") as? String
postTitle = aDecoder.decodeObjectForKey("postTitle") as? String
postText = aDecoder.decodeObjectForKey("postText") as? String
postImages = aDecoder.decodeObjectForKey("postImages") as? [PHAsset]
super.init()
}
override init() {
super.init()
}
}
当我实现下一个函数时:
func savePosts() {
let data = NSMutableData()
let archiver = NSKeyedArchiver(forWritingWithMutableData: data)
print("\(archiver)")
archiver.encodeObject(posts, forKey: "Posts")
archiver.finishEncoding()
data.writeToFile(dataFilePath(), atomically: true)
}
我有一个错误:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[PHAsset encodeWithCoder:]: unrecognized selector sent to instance 0x7fb6dc01a050'
在我将PHAsset添加到encodeWithCoder函数之前,一切都正常。但是PHAsset不符合NSCoder协议怎么办?