NSCoder:发送到实例的无法识别的选择器
NSCoder: Unrecognized selector sent to instance
所以,我是 iOS 的新手,一直在使用在线教程来学习该平台。我使用的教程是使用 iOS 9 构建应用程序,而我使用的是 iOS 10。因此,我在标题问题中有 运行。这是一些代码:
CalendarEvent.swift
class CalendarEvent : NSObject {
var title : String
var dateString : String
init(withTitle t : String, andDateString ds : String) {
title = t
dateString = ds
}
init(withCoder coder : NSCoder) {
dateString = coder.decodeObject(forKey: "dateString") as! String
title = coder.decodeObject(forKey: "title") as! String
}
func encodeWithCoder(coder : NSCoder) {
coder.encode(dateString, forKey: "dateString")
coder.encode(title, forKey: "title")
}
}
以下代码段来自另一个 class,其中出现错误:
SingleDayTableViewController.swift
@IBAction func addButtonPressed(sender : UIBarButtonItem) {
let newEvent = "Test Event \(events.count + 1)"
let defaultsKey = "\(monthNumber) - \(dayNumber)"
let ce = CalendarEvent(withTitle: newEvent, andDateString: defaultsKey)
let encodedCE = NSKeyedArchiver.archivedData(withRootObject: ce) // This line breaks the program.
events.append(encodedCE as AnyObject)
UserDefaults.standard.set(events, forKey: defaultsKey)
UserDefaults.standard.synchronize()
tableView.reloadData()
}
我不确定为什么会这样,但希望这里有人能为我解决问题。我将在下面包含堆栈跟踪。
2017-02-08 13:57:49.026 myCalendar[80042:1651988] -[myCalendar.CalendarEvent encodeWithCoder:]: unrecognized selector sent to instance 0x600000073440
2017-02-08 13:57:49.059 myCalendar[80042:1651988]
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[myCalendar.CalendarEvent encodeWithCoder:]: unrecognized selector sent to instance 0x600000073440'
*** First throw call stack:
(
0 CoreFoundation 0x00000001104b6d4b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x000000010ff1821e objc_exception_throw + 48
2 CoreFoundation 0x0000000110526f04 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3 CoreFoundation 0x000000011043c005 ___forwarding___ + 1013
4 CoreFoundation 0x000000011043bb88 _CF_forwarding_prep_0 + 120
5 Foundation 0x000000010fa4fcac _encodeObject + 1263
6 Foundation 0x000000010fa84a22 +[NSKeyedArchiver archivedDataWithRootObject:] + 156
7 myCalendar 0x000000010f923504 _TFC10myCalendar28SingleDayTableViewController16addButtonPressedfT6senderCSo15UIBarButtonItem_T_ + 980
8 myCalendar 0x000000010f9239aa _TToFC10myCalendar28SingleDayTableViewController16addButtonPressedfT6senderCSo15UIBarButtonItem_T_ + 58
9 UIKit 0x00000001108dc8bc -[UIApplication sendAction:to:from:forEvent:] + 83
10 UIKit 0x0000000110d1e4a1 -[UIBarButtonItem(UIInternal) _sendAction:withEvent:] + 149
11 UIKit 0x00000001108dc8bc -[UIApplication sendAction:to:from:forEvent:] + 83
12 UIKit 0x0000000110a62c38 -[UIControl sendAction:to:forEvent:] + 67
13 UIKit 0x0000000110a62f51 -[UIControl _sendActionsForEvents:withEvent:] + 444
14 UIKit 0x0000000110a630db -[UIControl _sendActionsForEvents:withEvent:] + 838
15 UIKit 0x0000000110a61e4d -[UIControl touchesEnded:withEvent:] + 668
16 UIKit 0x000000011094a545 -[UIWindow _sendTouchesForEvent:] + 2747
17 UIKit 0x000000011094bc33 -[UIWindow sendEvent:] + 4011
18 UIKit 0x00000001108f89ab -[UIApplication sendEvent:] + 371
19 UIKit 0x00000001219f2481 -[UIApplicationAccessibility sendEvent:] + 93
20 UIKit 0x00000001110e572d __dispatchPreprocessedEventFromEventQueue + 3248
21 UIKit 0x00000001110de463 __handleEventQueue + 4879
22 CoreFoundation 0x000000011045b761 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
23 CoreFoundation 0x000000011044098c __CFRunLoopDoSources0 + 556
24 CoreFoundation 0x000000011043fe76 __CFRunLoopRun + 918
25 CoreFoundation 0x000000011043f884 CFRunLoopRunSpecific + 420
26 GraphicsServices 0x0000000114b45a6f GSEventRunModal + 161
27 UIKit 0x00000001108dac68 UIApplicationMain + 159
28 myCalendar 0x000000010f924caf main + 111
29 libdyld.dylib 0x0000000113bb568d start + 1
30 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
在一些用户的帮助和一些额外的搜索下,我能够找出问题的原因。
根据前面提到的用户的建议,我将 NSCoding
协议添加到我的 CalendarEvent
class。
class CalendarEvent : NSObject, NSCoding {
....
}
不幸的是,这并没有完全解决问题,但这是一个开始。一些错误和快速搜索告诉我我必须改变这个:
init(withCoder coder : NSCoder){
....
}
func encodeWithCoder(coder : NSCoder){
....
}
对此:
required init(coder : NSCoder) {
....
}
func encode(with aCoder : NSCoder) {
....
}
进行这些更改后,我的代码 运行 完全没问题。
所以,我是 iOS 的新手,一直在使用在线教程来学习该平台。我使用的教程是使用 iOS 9 构建应用程序,而我使用的是 iOS 10。因此,我在标题问题中有 运行。这是一些代码:
CalendarEvent.swift
class CalendarEvent : NSObject {
var title : String
var dateString : String
init(withTitle t : String, andDateString ds : String) {
title = t
dateString = ds
}
init(withCoder coder : NSCoder) {
dateString = coder.decodeObject(forKey: "dateString") as! String
title = coder.decodeObject(forKey: "title") as! String
}
func encodeWithCoder(coder : NSCoder) {
coder.encode(dateString, forKey: "dateString")
coder.encode(title, forKey: "title")
}
}
以下代码段来自另一个 class,其中出现错误:
SingleDayTableViewController.swift
@IBAction func addButtonPressed(sender : UIBarButtonItem) {
let newEvent = "Test Event \(events.count + 1)"
let defaultsKey = "\(monthNumber) - \(dayNumber)"
let ce = CalendarEvent(withTitle: newEvent, andDateString: defaultsKey)
let encodedCE = NSKeyedArchiver.archivedData(withRootObject: ce) // This line breaks the program.
events.append(encodedCE as AnyObject)
UserDefaults.standard.set(events, forKey: defaultsKey)
UserDefaults.standard.synchronize()
tableView.reloadData()
}
我不确定为什么会这样,但希望这里有人能为我解决问题。我将在下面包含堆栈跟踪。
2017-02-08 13:57:49.026 myCalendar[80042:1651988] -[myCalendar.CalendarEvent encodeWithCoder:]: unrecognized selector sent to instance 0x600000073440
2017-02-08 13:57:49.059 myCalendar[80042:1651988]
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[myCalendar.CalendarEvent encodeWithCoder:]: unrecognized selector sent to instance 0x600000073440'
*** First throw call stack:
(
0 CoreFoundation 0x00000001104b6d4b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x000000010ff1821e objc_exception_throw + 48
2 CoreFoundation 0x0000000110526f04 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3 CoreFoundation 0x000000011043c005 ___forwarding___ + 1013
4 CoreFoundation 0x000000011043bb88 _CF_forwarding_prep_0 + 120
5 Foundation 0x000000010fa4fcac _encodeObject + 1263
6 Foundation 0x000000010fa84a22 +[NSKeyedArchiver archivedDataWithRootObject:] + 156
7 myCalendar 0x000000010f923504 _TFC10myCalendar28SingleDayTableViewController16addButtonPressedfT6senderCSo15UIBarButtonItem_T_ + 980
8 myCalendar 0x000000010f9239aa _TToFC10myCalendar28SingleDayTableViewController16addButtonPressedfT6senderCSo15UIBarButtonItem_T_ + 58
9 UIKit 0x00000001108dc8bc -[UIApplication sendAction:to:from:forEvent:] + 83
10 UIKit 0x0000000110d1e4a1 -[UIBarButtonItem(UIInternal) _sendAction:withEvent:] + 149
11 UIKit 0x00000001108dc8bc -[UIApplication sendAction:to:from:forEvent:] + 83
12 UIKit 0x0000000110a62c38 -[UIControl sendAction:to:forEvent:] + 67
13 UIKit 0x0000000110a62f51 -[UIControl _sendActionsForEvents:withEvent:] + 444
14 UIKit 0x0000000110a630db -[UIControl _sendActionsForEvents:withEvent:] + 838
15 UIKit 0x0000000110a61e4d -[UIControl touchesEnded:withEvent:] + 668
16 UIKit 0x000000011094a545 -[UIWindow _sendTouchesForEvent:] + 2747
17 UIKit 0x000000011094bc33 -[UIWindow sendEvent:] + 4011
18 UIKit 0x00000001108f89ab -[UIApplication sendEvent:] + 371
19 UIKit 0x00000001219f2481 -[UIApplicationAccessibility sendEvent:] + 93
20 UIKit 0x00000001110e572d __dispatchPreprocessedEventFromEventQueue + 3248
21 UIKit 0x00000001110de463 __handleEventQueue + 4879
22 CoreFoundation 0x000000011045b761 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
23 CoreFoundation 0x000000011044098c __CFRunLoopDoSources0 + 556
24 CoreFoundation 0x000000011043fe76 __CFRunLoopRun + 918
25 CoreFoundation 0x000000011043f884 CFRunLoopRunSpecific + 420
26 GraphicsServices 0x0000000114b45a6f GSEventRunModal + 161
27 UIKit 0x00000001108dac68 UIApplicationMain + 159
28 myCalendar 0x000000010f924caf main + 111
29 libdyld.dylib 0x0000000113bb568d start + 1
30 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
在一些用户的帮助和一些额外的搜索下,我能够找出问题的原因。
根据前面提到的用户的建议,我将 NSCoding
协议添加到我的 CalendarEvent
class。
class CalendarEvent : NSObject, NSCoding {
....
}
不幸的是,这并没有完全解决问题,但这是一个开始。一些错误和快速搜索告诉我我必须改变这个:
init(withCoder coder : NSCoder){
....
}
func encodeWithCoder(coder : NSCoder){
....
}
对此:
required init(coder : NSCoder) {
....
}
func encode(with aCoder : NSCoder) {
....
}
进行这些更改后,我的代码 运行 完全没问题。