2

I am trying to add an "Event" through an @IOButton using NSUserDefaults.

Before I upgraded to Swift 3 my code was running fine. Using the "convert" helper from Xcode, I was still able to run the code no problems.

I have further updated the version to meet the Swift 3 syntax and now I am getting an error have updated the syntax and I cannot figure out why I am getting an error when I try to encode my object.

Code converted through Xcode that runs without an error: //object import Foundation 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()
}

Updated code that gives an error:

//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()

}

Console Output:

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)

What am I no seeing????

1 Answer 1

1
func encodeWithCoder(coder : NSCoder) {

has been renamed in Swift 3 to

func encode(with aCoder: NSCoder) 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.