遵守?NSSecureCoding協議
importFoundation
class ZHFilterIndexPath: NSObject,NSSecureCoding {
? ? static var supportsSecureCoding: Bool { return true } // 需要添加這個靜態(tài)屬性
? ? varcolumn:Int?
? ? varrow:Int?
? ? varitem:Int?
? ? varselected:Bool=true
? ? vartitle:String=""
?//? fileprivate convenience
?? init(_title:String="",_selected:Bool,_column:Int,_row:Int?,_item:Int? =nil) {
? ? ? ? //self.init()
? ? ? ? self.column = column
? ? ? ? self.row = row
? ? ? ? self.item = item
? ? ? ? self.selected = selected
? ? ? ? self.title = title
? ? }
? ? funcencode(with coder:NSCoder) {
? ? ? ? coder.encode(column,forKey:"column")
? ? ? ? coder.encode(row,forKey:"row")
? ? ? ? coder.encode(item,forKey:"item")
? ? ? ? coder.encode(selected,forKey:"selected")
? ? ? ? coder.encode(title,forKey:"title")
? ? }
? ? requiredinit?(coder:NSCoder) {
? ? ? ? self.column = coder.decodeObject(forKey:"column")as?Int
? ? ? ? self.row = coder.decodeObject(forKey:"row")as?Int
? ? ? ? self.item = coder.decodeObject(forKey:"item")as?Int
? ? ? ? self.selected = coder.decodeBool(forKey:"selected")?
? ? ? ? self.title = coder.decodeObject(forKey:"title")as?String??""
? ? }
? ? overridefuncisEqual(_object:Any?) ->Bool{
? ? ? ? guardobjectisZHFilterIndexPathelse{
? ? ? ? ? ? returnfalse
? ? ? ? }
? ? ? ? letnewObj = objectas!ZHFilterIndexPath
? ? ? ? ifself.column == newObj.column &&self.row == newObj.row &&self.item == newObj.item &&self.title == newObj.title {
? ? ? ? ? ? returntrue
? ? ? ? }
? ? ? ? return false
? ? }
? ? override var description: String{
? ? ? ? get{
? ? ? ? ? ? return"ZHFilterIndexPath:(title:\(title)selected:\(selected)column:\(column ??-1),row:\(row ??-1),item:\(item ??-1))"
? ? ? ? }
? ? }
? ? classfuncindexOf(title:String="",selected:Bool,_column:Int,_row:Int?,_item:Int? =nil) ->ZHFilterIndexPath{
? ? ? ? letindexPath = ZHFilterIndexPath.init(title,selected,column, row, item)
? ? ? ? returnindexPath
? ? }
}
存儲方式直接存儲自定義對象
?? ? ? let manager = FileManager.default
? ? ? ? var url = manager.urls(for: .documentDirectory, in: .userDomainMask).first
? ? ? ? url?.appendPathComponent("path.txt")
? ? ? ? let datawrite =? NSKeyedArchiver.archivedData(withRootObject:index)
? ? ? ? try? datawrite.write(to: url!)
? ? ? ? if let dataread? = try? Data(contentsOf: url!){
? ? ? ? ? ? let array = NSKeyedUnarchiver.unarchiveObject(with: dataread) as! ZHFilterIndexPath
?? ? ? ? ? ? ? print("********\(array)******\(selectedCollectionViewIndexPaths)")
? ? ? ? }
自定義對象加入數組后存儲
? ? ? ? ? ? UserDefaults.standard.setValue(NSKeyedArchiver.archivedData(withRootObject: selectedCollectionViewIndexPaths), forKey:"personssub")
? ? ? ? ? ? UserDefaults.standard.synchronize()
? ? ? ? //unarchiveObjectWithData(userDefaults.objectForKey("persons")
? ? ? ? ? ? letarray = NSKeyedUnarchiver.unarchiveObject(with: UserDefaults.standard.object(forKey:"personssub")as!Data)as! [ZHFilterIndexPath]
? ? ? ? ? ? print("^^^^^^^^^^^^^^^^^^^^^^^^^^\(array)")