我有这样的代码:
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'在我向encodeWithCoder函数添加PHAsset之前,一切都运行得很好。PHAsset是否符合NSCoder协议?
发布于 2017-03-02 22:06:22
不需要将PHAsset封装到符合NSCoding的对象中。
每个PHAsset都有一个.localIdentifier,您可以将其另存为字符串,在恢复资产时,使用fetchAssetCollectionsWithLocalIdentifiers:options:检索具有本地标识符的资产
https://stackoverflow.com/questions/35653443
复制相似问题