在XCode 8中出错 -

时间:2017-07-06 18:49:22

标签: swift xcode8

在我的学校项目中出现此错误,我不知道在哪里看: (介绍:在这个单元中,我们将构建一个ToDo List App。我们将在Swift中探索Master-Detail视图以及更复杂的代码。

成功完成本单位将使您:

扩展本机Swift类以添加您自己的功能 存储与应用程序关联的数据 使用SplitViewController创建Master-Detail应用程序 响应用户请求 具有故事板序列的视图控制器之间的转换 显示对话框以响应用户交互 读数

完成第4章并完成本单元中的内容后,您应该已准备好完成作业。 )

2017-07-06 11:28:59.938 ToDoList[1154:162653] -[ToDoList.ToDoItem encodeWithCoder:]: unrecognized selector sent to instance 0x60000027da40
2017-07-06 11:28:59.951 ToDoList[1154:162653] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ToDoList.ToDoItem encodeWithCoder:]: unrecognized selector sent to instance 0x60000027da40'
*** First throw call stack:
(
0   CoreFoundation                      0x0000000109336b0b __exceptionPreprocess + 171
1   libobjc.A.dylib                     0x0000000108d9b141 objc_exception_throw + 48
2   CoreFoundation                      0x00000001093a6134 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3   CoreFoundation                      0x00000001092bd840 ___forwarding___ + 1024
4   CoreFoundation                      0x00000001092bd3b8 _CF_forwarding_prep_0 + 120
5   Foundation                          0x00000001088d9b16 _encodeObject + 1224
6   Foundation                          0x00000001088daeb0 -[NSKeyedArchiver _encodeArrayOfObjects:forKey:] + 439
7   Foundation                          0x00000001088d9b16 _encodeObject + 1224
8   Foundation                          0x000000010890d3fe +[NSKeyedArchiver archivedDataWithRootObject:] + 156
9   ToDoList                            0x00000001087aebcc _TZFC8ToDoList15ToDoListManagerP33_CE0D50EB39E1FB8DDE4ED96B00EE34055storefT5itemsGSaCS_8ToDoItem__T_ + 140
10  ToDoList                            0x00000001087ae9ef _TFC8ToDoList15ToDoListManager7addItemfT4itemCS_8ToDoItem_T_ + 207
11  ToDoList                            0x00000001087b0ad7 _TFFC8ToDoList20MasterViewController16addButtonPressedFT6senderPs9AnyObject__T_U0_FCSo13UIAlertActionT_ + 695
12  ToDoList                            0x00000001087b07dc _TTRXFo_oCSo13UIAlertAction__XFdCb_dS___ + 60
13  UIKit                               0x0000000109ac2f45 -[UIAlertController _invokeHandlersForAction:] + 105
14  UIKit                               0x0000000109ac38b0 __85-[UIAlertController _dismissAnimated:triggeringAction:triggeredByPopoverDimmingView:]_block_invoke.454 + 16
15  UIKit                               0x00000001098d6177 -[UIPresentationController transitionDidFinish:] + 1351
16  UIKit                               0x00000001098d9af6 __56-[UIPresentationController runTransitionForCurrentState]_block_invoke_2 + 183
17  UIKit                               0x000000010a289d1c -[_UIViewControllerTransitionContext completeTransition:] + 102
18  UIKit                               0x0000000109813024 -[UIViewAnimationBlockDelegate _didEndBlockAnimation:finished:context:] + 527
19  UIKit                               0x00000001097e6257 -[UIViewAnimationState sendDelegateAnimationDidStop:finished:] + 222
20  UIKit                               0x00000001097e6796 -[UIViewAnimationState animationDidStop:finished:] + 136
21  QuartzCore                          0x000000010e7d068e _ZN2CA5Layer23run_animation_callbacksEPv + 306
22  libdispatch.dylib                   0x000000010c95f05c _dispatch_client_callout + 8
23  libdispatch.dylib                   0x000000010c94040b _dispatch_main_queue_callback_4CF + 411
24  CoreFoundation                      0x00000001092fb909 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
25  CoreFoundation                      0x00000001092c1ae4 __CFRunLoopRun + 2164
26  CoreFoundation                      0x00000001092c1016 CFRunLoopRunSpecific + 406
27  GraphicsServices                    0x000000010d916a24 GSEventRunModal + 62
28  UIKit                               0x000000010975a134 UIApplicationMain + 159
29  ToDoList                            0x00000001087ac257 main + 55
30  libdyld.dylib                       0x000000010c9ab65d start + 1
31  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

代码是:

import Foundation

class ToDoItem: NSCoder {

    let itemDescription: String
    let completed: Bool
    let dataAdded: NSDate

    init(description: String) {

        self.itemDescription = description
        self.completed = false
        self.dataAdded = NSDate()
    }


    func getDateAdded() -> String {
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd"
        return formatter.string(from:self.dataAdded as Date)
    }

    //Mark: - NSCoding -

    required init(coder aDecoder: NSCoder) {
        self.itemDescription = aDecoder.decodeObject(forKey: "description") as! String
        self.completed = aDecoder.decodeObject(forKey: "completed") as! Bool
        self.dataAdded = aDecoder.decodeObject(forKey: "dateAdded") as! NSDate
    }

    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encode(self.itemDescription, forKey: "description")
        aCoder.encode(self.completed, forKey: "completed")
        aCoder.encode(self.dataAdded, forKey: "dateAdded")
    }
}

并且由此调用:

import Foundation

class ToDoListManager {

    private static let userDefaults = UserDefaults.standard
    private static let listKey = "ToDoListKey"

    var toDoItems: [ToDoItem]

    init() {
        toDoItems = [ToDoItem]()

        //self.toDoItems.append(ToDoItem(description: "Get Milk"))
        //self.toDoItems.append(ToDoItem(description: "Walk Dog"))
        //self.toDoItems.append(ToDoItem(description: "Pay Bills"))

        //self.userDefaults.set("test", forKey: listKey)

        self.toDoItems = ToDoListManager.retrieve() ?? [ToDoItem]()

    }

    func getItems()  -> [ToDoItem]{
        return self.toDoItems
    }

    func numberItems() -> Int {
        return self.toDoItems.count

    }

    func addItem(item: ToDoItem) {
        self.toDoItems.append(item)        
        ToDoListManager.store(items: self.toDoItems)

    }

    func removeItem(index: Int) {
        self.toDoItems.remove(at: index)


    }

    func getFirstItem() -> ToDoItem? {
        return self.toDoItems.first
    }

    private class func store(items: [ToDoItem]) {
        let encodedObject = NSKeyedArchiver.archivedData(withRootObject: items)
        userDefaults.set(encodedObject, forKey: listKey)
        userDefaults.synchronize()

    }

    private class func retrieve() -> [ToDoItem]? {
        if let decodedObject = userDefaults.object(forKey: listKey) as? NSData {
            return NSKeyedUnarchiver.unarchiveObject(with: decodedObject as Data) as? [ToDoItem]
        }
        return nil
    }

}

0 个答案:

没有答案