我有一个自定义类,用于映射,如下所示。
class UserSaved {
var Id : String
var UserName : String
var profileURL : String
var fullNameUser : String
func encode(with aCoder: NSCoder) {
aCoder.encode(self.Id, forKey: "Id")
aCoder.encode(self.UserName, forKey: "UserName")
aCoder.encode(self.profileURL, forKey: "profileURL")
aCoder.encode(self.fullNameUser, forKey: "fullNameUser")
}
required init?(coder aDecoder: NSCoder) {
self.Id = aDecoder.decodeObject(forKey: "Id") as! String
self.UserName = aDecoder.decodeObject(forKey: "UserName") as! String
self.profileURL = aDecoder.decodeObject(forKey: "profileURL") as! String
self.fullNameUser = aDecoder.decodeObject(forKey: "fullNameUser") as! String
// self.init(my_id: self.Id, my_username: self.UserName, profile_url: self.profileURL, full_name: self.fullNameUser)
}
init(my_id: String, my_username: String, profile_url: String, full_name : String) {
self.Id = my_id
self.UserName = my_username
self.profileURL = profile_url
self.fullNameUser = full_name
}
}
现在我将不同的模型对象添加到两个不同的数组中,如下所示,在这两个数组下面可能有一些共同的条目
//////array 1
let arrayTemp = dic.value(forKey: "data") as! NSArray
for i in 0..<arrayTemp.count {
let dic = arrayTemp.object(at: i) as! NSDictionary
let strId = String(dic.value(forKey: "pk") as! Int)
let instaName = dic.value(forKey: "username") as! String
let profileURL = dic.value(forKey: "profile_pic_url") as! String
let fullName = dic.value(forKey: "full_name") as! String
let person1 = UserSaved(insta_id: strId, insta_username: instaName,profile_url: profileURL,full_name: fullName)
self.arr1.append(person1)
}
//////array 2
let arrayTemp = dic.value(forKey: "data") as! NSArray
for i in 0..<arrayTemp.count {
let dic = arrayTemp.object(at: i) as! NSDictionary
let strId = String(dic.value(forKey: "pk") as! Int)
let instaName = dic.value(forKey: "username") as! String
let profileURL = dic.value(forKey: "profile_pic_url") as! String
let fullName = dic.value(forKey: "full_name") as! String
let person2 = UserSaved(insta_id: strId, insta_username: instaName,profile_url: profileURL,full_name: fullName)
self.arr2.append(person2)
}
现在,我正在使用下面的代码来操作上面的数组,效果很好
let setA = Set(self.arr1)
let setB = Set(self.arr2)
let inBothAAndB = setA.intersection(setB)
let inAButNotB = setA.subtracting(setB)
let inBButNotA = setB.subtracting(setA)
现在,当我尝试使用NSKeyedArchiver.archivedData
将数组保存到用户默认设置中时。它给了我如下错误
2018-10-09 12:27:46.375587+0530 Insta_Analytics[12764:181604] *** NSForwarding: warning: object 0x6100000f0500 of class
'Insta_Analytics.UserSaved' does not implement methodSignatureForSelector: -- trouble ahead
Unrecognized selector -[Insta_Analytics.UserSaved replacementObjectForKeyedArchiver:]
2018-10-09 12:27:46.376141+0530 Insta_Analytics[12764:181604]
Unrecognized selector -[Insta_Analytics.UserSaved replacementObjectForKeyedArchiver:]
这是我将数组保存到userdefaults
let encodedData = NSKeyedArchiver.archivedData(withRootObject: self.arr1)
UserDefaults.standard.set(encodedData, forKey: "arr_store")
现在,如果我更改自定义映射模型UserSaved
,则继承自NSCoding
和NSObject
。可以,但是操作数据的时间不起作用
NSCoding
保存数组,同时intersection
和subtracting
应该可以工作
答案 0 :(得分:1)
要使用NSCoder
,该类必须是NSObject
的子类
class UserSaved : NSObject, NSCoding {
但是在您的情况下,我建议使用Codable
并将符合性添加到Equatable
class UserSaved : Codable, Equatable {
删除init(coder
和encode(with
方法并序列化数据
let encodedData = PropertyListEncoder().encode(self.arr1)
并且不要在Swift中使用NSArray / NSDictionary
。使用本机类型。并且不要使用value(forKey
。使用密钥下标。
要解码数据,请使用
if let data = UserDefaults.standard.data(forKey: "arr_store") {
do {
let array = try PropertyListDecoder().decode([UserSaved].self, from: data)
} catch { print(error) }
}
答案 1 :(得分:0)
更新您的类UserSaved,以实现以下内容:
class UserSaved: NSObject, NSCoding, Hashable {
// ...