NSKeyedArchiver - 无法识别的选择器发送到实例错误
NSKeyedArchiver - Unrecognized selector sent to instance error
我在使用 NSKeyedArchiver.archivedData
时遇到 unrecognized selector sent to instance
错误。
我试图将 JSON 输出解析存储到自定义 class 对象,并在成功登录后将其存储在 NSUserDefaults
中。我想在其他控制器中使用这些详细信息来进行其他 POST
调用。但是在将它们保存在我的 LoginController
中时,我遇到了这个错误。有人可以帮助解决这个问题吗?
我的Class:
class UserDetails : NSObject,NSCoding {
var token:String?
var agentId: Int?
var userId:Int?
//to directly get values from JSON output
init?(user: [String: Any]) {
guard let token = user["token"] as? String,
let agentId = user["agent_id"] as? Int,
let userId = user["user_id"] as? Int
else{
return nil
}
self.token = token;
self.agentId = agentId;
self.userId = userId;
}
init(pToken:String,pAgentId:Int,pUserId:Int)
{
token = pToken;
agentId = pAgentId;
userId = pUserId;
}
//TO SAVE IN NSUSERDEFAULTS
required init?(coder aDecoder: NSCoder){
self.token = aDecoder.decodeObject(forKey: "token") as? String
self.agentId = aDecoder.decodeObject(forKey: "agentId") as? Int
self.userId = aDecoder.decodeObject(forKey: "userId") as? Int
return self
}
public func encode(with aCoder: NSCoder) {
aCoder.encode(token,forKey:"token")
aCoder.encode(agentId,forKey:"agentId")
}
}
成功登录后,我将存储此详细信息。
登录视图控制器:
if let userDict = json["user"] as? [String:Any]
{
guard let userObject = UserDetails(user:userDict) else {
print("Failed to create user from dictionary")
return
}
DispatchQueue.main.async {
self.saveUserItemToDefault(userObject: userObject);
self.dismiss(animated: true, completion: nil)
self.performSegue(withIdentifier: "loginSuccessIdentifier", sender: userObject)
}
}
func saveUserItemToDefault(userObject:UserDetails) -> Void {
let encodedToken = NSKeyedArchiver.archivedData(withRootObject: userObject.token) //error is thrown on the first archiver call
let encodedAgentId = NSKeyedArchiver.archivedData(withRootObject: userObject.agentId)
let encodedUserId = NSKeyedArchiver.archivedData(withRootObject: userObject.userId)
let encodedArray:[NSData] = [encodedToken as NSData,encodedAgentId as NSData,encodedUserId as NSData]
self.prefs.set(encodedArray, forKey: "UserDetailsItem")
self.prefs.synchronize()
}
错误信息:
2016-12-05 18:08:57.917 ABCAPP[2800:183556] -[_SwiftValue encodeWithCoder:]: unrecognized selector sent to instance 0x600000079f00
2016-12-05 18:08:57.920 ABCAPP[2800:183556] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_SwiftValue encodeWithCoder:]: unrecognized selector sent to instance 0x600000079f00'
*** First throw call stack:
(
0 CoreFoundation 0x0000000104b5e34b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x00000001045bf21e objc_exception_throw + 48
2 CoreFoundation 0x0000000104bcdf34 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3 CoreFoundation 0x0000000104ae3c15 ___forwarding___ + 1013
4 CoreFoundation 0x0000000104ae3798 _CF_forwarding_prep_0 + 120
5 Foundation 0x00000001040f855c _encodeObject + 1263
6 Foundation 0x000000010412d29a +[NSKeyedArchiver archivedDataWithRootObject:] + 156
7 ABCAPP 0x0000000103fa8d4b _TFC15ABCAPP19LoginViewController21saveUserItemToDefaultfT10userObjectCS_11UserDetails_T_ + 235
8 ABCAPP 0x0000000103face3d _TFFFC15ABCAPP19LoginViewController10checkLoginFPs9AnyObject_T_U_FTGSqV10Foundation4Data_GSqCSo11URLResponse_GSqPs5Error___T_U_FT_T_ + 77
9 ABCAPP 0x0000000103f9d627 _TTRXFo___XFdCb___ + 39
10 libdispatch.dylib 0x0000000108112980 _dispatch_call_block_and_release + 12
11 libdispatch.dylib 0x000000010813c0cd _dispatch_client_callout + 8
12 libdispatch.dylib 0x000000010811c8d6 _dispatch_main_queue_callback_4CF + 406
13 CoreFoundation 0x0000000104b224f9 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
14 CoreFoundation 0x0000000104ae7f8d __CFRunLoopRun + 2205
15 CoreFoundation 0x0000000104ae7494 CFRunLoopRunSpecific + 420
16 GraphicsServices 0x000000010910da6f GSEventRunModal + 161
17 UIKit 0x0000000104f81f34 UIApplicationMain + 159
18 ABCAPP 0x0000000103faff2f main + 111
19 libdyld.dylib 0x000000010818868d start + 1
20 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
问题可能是因为您正在尝试归档一个可选的并且 Optional
没有 encodeWithCoder
方法(因此出现错误消息)。
尝试这样做:
public func encode(with aCoder: NSCoder) {
if let token = token {
aCoder.encode(token,forKey:"token")
}
if let agentId = agentId {
aCoder.encode(agentId,forKey:"agentId")
}
}
除了@JPetric 已经说过的(我可能是真的)之外,当您遇到此类异常时,一般来说,问题是您尝试归档一种无法在 Objective-C 中表示的数据类型.这可以是一个可选的,一个 swift 枚举,一个 swift 闭包等。添加一个异常断点来查看编码失败的实际对象,然后找到一种方法来代替 Objective-C对象。
根据 JPetric
和 Valentin Radu
的输入,我对代码进行了一些修改,错误已被删除。
以下是我的解决方案:
class UserDetails : NSObject,NSCoding {
//var token:String?
var token:String!
//var agentId: Int?
var agentId: Int!
//var userId:Int?
var userId:Int!
//to directly get values from JSON output
init?(user: [String: Any]) {
guard let token = user["token"] as? String,
let agentId = user["agent_id"] as? Int,
let userId = user["user_id"] as? Int
else{
return nil
}
self.token = token;
self.agentId = agentId;
self.userId = userId;
}
init(pToken:String,pAgentId:Int,pUserId:Int)
{
token = pToken;
agentId = pAgentId;
userId = pUserId;
}
//TO SAVE IN NSUSERDEFAULTS
required init?(coder aDecoder: NSCoder){
self.token = aDecoder.decodeObject(forKey: "token") as? String
self.agentId = aDecoder.decodeObject(forKey: "agentId") as? Int
self.userId = aDecoder.decodeObject(forKey: "userId") as? Int
return self
}
public func encode(with aCoder: NSCoder) {
aCoder.encode(token,forKey:"token")
aCoder.encode(agentId,forKey:"agentId")
}
}
我在使用 NSKeyedArchiver.archivedData
时遇到 unrecognized selector sent to instance
错误。
我试图将 JSON 输出解析存储到自定义 class 对象,并在成功登录后将其存储在 NSUserDefaults
中。我想在其他控制器中使用这些详细信息来进行其他 POST
调用。但是在将它们保存在我的 LoginController
中时,我遇到了这个错误。有人可以帮助解决这个问题吗?
我的Class:
class UserDetails : NSObject,NSCoding {
var token:String?
var agentId: Int?
var userId:Int?
//to directly get values from JSON output
init?(user: [String: Any]) {
guard let token = user["token"] as? String,
let agentId = user["agent_id"] as? Int,
let userId = user["user_id"] as? Int
else{
return nil
}
self.token = token;
self.agentId = agentId;
self.userId = userId;
}
init(pToken:String,pAgentId:Int,pUserId:Int)
{
token = pToken;
agentId = pAgentId;
userId = pUserId;
}
//TO SAVE IN NSUSERDEFAULTS
required init?(coder aDecoder: NSCoder){
self.token = aDecoder.decodeObject(forKey: "token") as? String
self.agentId = aDecoder.decodeObject(forKey: "agentId") as? Int
self.userId = aDecoder.decodeObject(forKey: "userId") as? Int
return self
}
public func encode(with aCoder: NSCoder) {
aCoder.encode(token,forKey:"token")
aCoder.encode(agentId,forKey:"agentId")
}
}
成功登录后,我将存储此详细信息。
登录视图控制器:
if let userDict = json["user"] as? [String:Any]
{
guard let userObject = UserDetails(user:userDict) else {
print("Failed to create user from dictionary")
return
}
DispatchQueue.main.async {
self.saveUserItemToDefault(userObject: userObject);
self.dismiss(animated: true, completion: nil)
self.performSegue(withIdentifier: "loginSuccessIdentifier", sender: userObject)
}
}
func saveUserItemToDefault(userObject:UserDetails) -> Void {
let encodedToken = NSKeyedArchiver.archivedData(withRootObject: userObject.token) //error is thrown on the first archiver call
let encodedAgentId = NSKeyedArchiver.archivedData(withRootObject: userObject.agentId)
let encodedUserId = NSKeyedArchiver.archivedData(withRootObject: userObject.userId)
let encodedArray:[NSData] = [encodedToken as NSData,encodedAgentId as NSData,encodedUserId as NSData]
self.prefs.set(encodedArray, forKey: "UserDetailsItem")
self.prefs.synchronize()
}
错误信息:
2016-12-05 18:08:57.917 ABCAPP[2800:183556] -[_SwiftValue encodeWithCoder:]: unrecognized selector sent to instance 0x600000079f00
2016-12-05 18:08:57.920 ABCAPP[2800:183556] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_SwiftValue encodeWithCoder:]: unrecognized selector sent to instance 0x600000079f00'
*** First throw call stack:
(
0 CoreFoundation 0x0000000104b5e34b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x00000001045bf21e objc_exception_throw + 48
2 CoreFoundation 0x0000000104bcdf34 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3 CoreFoundation 0x0000000104ae3c15 ___forwarding___ + 1013
4 CoreFoundation 0x0000000104ae3798 _CF_forwarding_prep_0 + 120
5 Foundation 0x00000001040f855c _encodeObject + 1263
6 Foundation 0x000000010412d29a +[NSKeyedArchiver archivedDataWithRootObject:] + 156
7 ABCAPP 0x0000000103fa8d4b _TFC15ABCAPP19LoginViewController21saveUserItemToDefaultfT10userObjectCS_11UserDetails_T_ + 235
8 ABCAPP 0x0000000103face3d _TFFFC15ABCAPP19LoginViewController10checkLoginFPs9AnyObject_T_U_FTGSqV10Foundation4Data_GSqCSo11URLResponse_GSqPs5Error___T_U_FT_T_ + 77
9 ABCAPP 0x0000000103f9d627 _TTRXFo___XFdCb___ + 39
10 libdispatch.dylib 0x0000000108112980 _dispatch_call_block_and_release + 12
11 libdispatch.dylib 0x000000010813c0cd _dispatch_client_callout + 8
12 libdispatch.dylib 0x000000010811c8d6 _dispatch_main_queue_callback_4CF + 406
13 CoreFoundation 0x0000000104b224f9 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
14 CoreFoundation 0x0000000104ae7f8d __CFRunLoopRun + 2205
15 CoreFoundation 0x0000000104ae7494 CFRunLoopRunSpecific + 420
16 GraphicsServices 0x000000010910da6f GSEventRunModal + 161
17 UIKit 0x0000000104f81f34 UIApplicationMain + 159
18 ABCAPP 0x0000000103faff2f main + 111
19 libdyld.dylib 0x000000010818868d start + 1
20 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
问题可能是因为您正在尝试归档一个可选的并且 Optional
没有 encodeWithCoder
方法(因此出现错误消息)。
尝试这样做:
public func encode(with aCoder: NSCoder) {
if let token = token {
aCoder.encode(token,forKey:"token")
}
if let agentId = agentId {
aCoder.encode(agentId,forKey:"agentId")
}
}
除了@JPetric 已经说过的(我可能是真的)之外,当您遇到此类异常时,一般来说,问题是您尝试归档一种无法在 Objective-C 中表示的数据类型.这可以是一个可选的,一个 swift 枚举,一个 swift 闭包等。添加一个异常断点来查看编码失败的实际对象,然后找到一种方法来代替 Objective-C对象。
根据 JPetric
和 Valentin Radu
的输入,我对代码进行了一些修改,错误已被删除。
以下是我的解决方案:
class UserDetails : NSObject,NSCoding {
//var token:String?
var token:String!
//var agentId: Int?
var agentId: Int!
//var userId:Int?
var userId:Int!
//to directly get values from JSON output
init?(user: [String: Any]) {
guard let token = user["token"] as? String,
let agentId = user["agent_id"] as? Int,
let userId = user["user_id"] as? Int
else{
return nil
}
self.token = token;
self.agentId = agentId;
self.userId = userId;
}
init(pToken:String,pAgentId:Int,pUserId:Int)
{
token = pToken;
agentId = pAgentId;
userId = pUserId;
}
//TO SAVE IN NSUSERDEFAULTS
required init?(coder aDecoder: NSCoder){
self.token = aDecoder.decodeObject(forKey: "token") as? String
self.agentId = aDecoder.decodeObject(forKey: "agentId") as? Int
self.userId = aDecoder.decodeObject(forKey: "userId") as? Int
return self
}
public func encode(with aCoder: NSCoder) {
aCoder.encode(token,forKey:"token")
aCoder.encode(agentId,forKey:"agentId")
}
}