2
我想通過使用NSUserDefaults的@IOButton添加「事件」。錯誤在Swift 3中使用編碼器
在我升級到Swift 3之前,我的代碼運行良好。使用Xcode的「convert」助手,我仍然能夠運行代碼沒有問題。
我已經進一步更新版本以符合Swift 3語法,現在我得到一個錯誤已經更新了語法,我無法弄清楚爲什麼當我嘗試編碼我的對象時出現錯誤。
代碼通過無差錯運行Xcode的轉換:// 對象 進口基金會 類CalendarEvent:NSObject的{VAR 標題:字符串 VAR dateString:字符串
init(withTitle t : String, andDateString ds : String) {
title = t
dateString = ds
}
//get values out of NSUserDefaults
init(withCoder coder : NSCoder) {
dateString = coder.decodeObject(forKey: "dateString") as! String
title = coder.decodeObject(forKey: "title") as! String
}
//time to put CalendarEvent into NSUserDefaults Key-Value store
func encodeWithCoder(_ coder : NSCoder) {
coder.encode(dateString, forKey: "dateString")
coder.encode(title, forKey: "title")
}
}
//button action
@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)
events.append(encodedCE as AnyObject)
UserDefaults.standard.set(events, forKey: defaultsKey)
UserDefaults.standard.synchronize()
tableView.reloadData()
}
更新代碼,給出了一個錯誤:
//object
class CalendarEvent: NSObject {
var title : String
var dateString : String
init(withTitle t: String, andDateString ds: String) {
title = t
dateString = ds
}
required init(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")
}
}
//button action
@IBAction func addButtonPressed(sender: UIBarButtonItem){
let newEvent = "Test Event \(events.count+1)"
let defaultsKey = "\(monthNum)-\(dayNum)"
let ce = CalendarEvent(withTitle: newEvent, andDateString: defaultsKey)
// where the error occurs
let encodedCE = NSKeyedArchiver.archivedData(withRootObject: ce)
events.append(encodedCE as AnyObject)
UserDefaults.standard.object(forKey: defaultsKey)
UserDefaults.standard.synchronize()
tableView.reloadData()
}
控制檯輸出:
1-1
2016-10-07 19:39:26.152 Swift3_TableView_CalApp[1400:287793] -[Swift3_TableView_CalApp.CalendarEvent encodeWithCoder:]: unrecognized selector sent to instance 0x60800026ebc0
2016-10-07 19:39:26.171 Swift3_TableView_CalApp[1400:287793] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Swift3_TableView_CalApp.CalendarEvent encodeWithCoder:]: unrecognized selector sent to instance 0x60800026ebc0'
*** First throw call stack:
(
0 CoreFoundation 0x0000000102d3d34b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x000000010279e21e objc_exception_throw + 48
2 CoreFoundation 0x0000000102dacf34 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3 CoreFoundation 0x0000000102cc2c15 ___forwarding___ + 1013
4 CoreFoundation 0x0000000102cc2798 _CF_forwarding_prep_0 + 120
5 Foundation 0x00000001022d755c _encodeObject + 1263
6 Foundation 0x000000010230c29a +[NSKeyedArchiver archivedDataWithRootObject:] + 156
7 Swift3_TableView_CalApp 0x00000001021ad1b0 _TFC23Swift3_TableView_CalApp12SingleDayTVC16addButtonPressedfT6senderCSo15UIBarButtonItem_T_ + 976
8 Swift3_TableView_CalApp 0x00000001021ad7aa _TToFC23Swift3_TableView_CalApp12SingleDayTVC16addButtonPressedfT6senderCSo15UIBarButtonItem_T_ + 58
9 UIKit 0x0000000103162b88 -[UIApplication sendAction:to:from:forEvent:] + 83
10 UIKit 0x00000001035a384d -[UIBarButtonItem(UIInternal) _sendAction:withEvent:] + 149
11 UIKit 0x0000000103162b88 -[UIApplication sendAction:to:from:forEvent:] + 83
12 UIKit 0x00000001032e82b2 -[UIControl sendAction:to:forEvent:] + 67
13 UIKit 0x00000001032e85cb -[UIControl _sendActionsForEvents:withEvent:] + 444
14 UIKit 0x00000001032e8755 -[UIControl _sendActionsForEvents:withEvent:] + 838
15 UIKit 0x00000001032e74c7 -[UIControl touchesEnded:withEvent:] + 668
16 UIKit 0x00000001031d00d5 -[UIWindow _sendTouchesForEvent:] + 2747
17 UIKit 0x00000001031d17c3 -[UIWindow sendEvent:] + 4011
18 UIKit 0x000000010317ea33 -[UIApplication sendEvent:] + 371
19 UIKit 0x0000000103970b6d __dispatchPreprocessedEventFromEventQueue + 3248
20 UIKit 0x0000000103969817 __handleEventQueue + 4879
21 CoreFoundation 0x0000000102ce2311 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
22 CoreFoundation 0x0000000102cc759c __CFRunLoopDoSources0 + 556
23 CoreFoundation 0x0000000102cc6a86 __CFRunLoopRun + 918
24 CoreFoundation 0x0000000102cc6494 CFRunLoopRunSpecific + 420
25 GraphicsServices 0x00000001072e6a6f GSEventRunModal + 161
26 UIKit 0x0000000103160f34 UIApplicationMain + 159
27 Swift3_TableView_CalApp 0x00000001021a913f main + 111
28 libdyld.dylib 0x000000010636768d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
我什麼都沒見到?