我正在尝试添加"事件"通过@IOButton使用NSUserDefaults。
在升级到Swift 3之前,我的代码运行正常。使用"转换"来自Xcode的助手,我仍然能够运行代码没有问题。
我已经进一步更新了版本以满足Swift 3语法,现在我收到错误更新了语法,我无法弄清楚为什么我在尝试编码对象时出错。
通过Xcode转换的代码运行时没有错误: //宾语 进口基金会 class CalendarEvent:NSObject { var title:String var dateString:String
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)
我没看到什么????
答案 0 :(得分:0)
func encodeWithCoder(coder : NSCoder) {
已在Swift 3中重命名为
func encode(with aCoder: NSCoder)