【发布时间】:2015-04-22 11:46:21
【问题描述】:
我正在尝试归档一个包含一些成员的 photo() 对象数组。鉴于 NsCoding 协议已正确实现,以下代码能否成功归档(保存)数组中的数据并再次归档?
var dir = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) [0] as String
var dir2 = "/photo.plist"
//This method is suppose to archive objects if the array is not empty.
func archivePhoto ( ) {
let archiveName = self.dir + self.dir2
if ( photos.count > 0 ){
for photo in photos {
NSKeyedArchiver.archiveRootObject(photo, toFile: archiveName)
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Register cell classes
self.collectionView!.registerClass(PhotosCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
// Do any additional setup after loading the view.
archivePhoto()
let archiveName = self.dir + self.dir2
if let myArray = NSKeyedUnarchiver.unarchiveObjectWithFile(archiveName) as Array<Photo>? {
photos = myArray
self.collectionView?.reloadData()
}
}
【问题讨论】:
-
"下面的代码是否可以成功地归档(保存)数组中的数据并再次归档?目前它没有这样做。"如果它不这样做,那么显然不,它不会起作用。有什么问题?
-
@matt 很抱歉造成混乱,我将编辑我的问题。但是你能帮我指出我的错误吗
标签: swift nscoding nskeyedarchiver