Realm更新Swift无法识别的选择器发送到实例

时间:2017-02-10 08:17:26

标签: ios swift realm

我正在使用Realm制作一个简单的列表应用程序,但我在制作更新方法时遇到了麻烦。这是我的代码。

Task.swift

class Task: Object {
    dynamic var id = 0
    dynamic var name = ""
    dynamic var content = ""
    dynamic var createdDate = NSDate()

    override class func primaryKey() -> String? {
        return "id"
    }

    class func autoIncrement() -> Int{
        let realm = try! Realm()
        let TaskList = realm.objects(Task.self).sorted(byKeyPath: "id", ascending: true)
        let Last = TaskList.last
        if TaskList.count == 0{
            return 0
        } else{
            return Last!.id + 1
        }
}

}

TaskController.swift

static func updateTask(name:String, content:String, oldTask:Task){
    RealmController.updateTask(name:name, content:content, task: oldTask)
    //RealmController.getData(taskList: &appDelegate.task)
}

RealmController.swift

static func updateTask(name:String, content:String, task: Task){
    let taskId = task.id
    let RealTask = realm.objects(Task.self).filter("id == %@", taskId).first
    try! realm.write {
            RealTask?.name = name
            RealTask?.content = content
    }
}

错误消息。

  

2017-02-10 17:13:57.179 RealmTest1 [7156:9710210]    - [RealmTest1.EditViewController saveTask:]:无法识别的选择器发送到实例0x7fd06351eaa0 2017-02-10 17:13:57.183   RealmTest1 [7156:9710210] ***由于未捕获的异常而终止应用程序   'NSInvalidArgumentException',原因:' - [RealmTest1.EditViewController   saveTask:]:无法识别的选择器发送到实例0x7fd06351eaa0'

堆栈跟踪

0   CoreFoundation                      0x0000000107c00d4b __exceptionPreprocess + 171<br/>
1   libobjc.A.dylib                     0x000000010766221e objc_exception_throw + 48<br/>
2   CoreFoundation                      0x0000000107c70f04 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3   CoreFoundation                      0x0000000107b86005 ___forwarding___ + 1013<br/>
4   CoreFoundation                      0x0000000107b85b88 _CF_forwarding_prep_0 + 120<br/>
5   UIKit                               0x00000001080268bc -[UIApplication sendAction:to:from:forEvent:] + 83<br/>
6   UIKit                               0x00000001081acc38 -[UIControl sendAction:to:forEvent:] + 67<br/>
7   UIKit                               0x00000001081acf51 -[UIControl _sendActionsForEvents:withEvent:] + 444<br/>
8   UIKit                               0x00000001081abe4d -[UIControl touchesEnded:withEvent:] + 668<br/>
9   UIKit                               0x0000000108094545 -[UIWindow _sendTouchesForEvent:] + 2747<br/>
10  UIKit                               0x0000000108095c33 -[UIWindow sendEvent:] + 4011<br/>
11  UIKit                               0x00000001080429ab -[UIApplication sendEvent:] + 371<br/>
12  UIKit                               0x000000010882f72d __dispatchPreprocessedEventFromEventQueue + 3248<br/>
13  UIKit                               0x0000000108828463 __handleEventQueue + 4879<br/>
14  CoreFoundation                      0x0000000107ba5761 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17<br/>
15  CoreFoundation                      0x0000000107b8a98c __CFRunLoopDoSources0 + 556<br/>
16  CoreFoundation                      0x0000000107b89e76 __CFRunLoopRun + 918<br/>
17  CoreFoundation                      0x0000000107b89884 CFRunLoopRunSpecific + 420<br/>
18  GraphicsServices                    0x000000010d8caa6f GSEventRunModal + 161<br/>
19  UIKit                               0x0000000108024c68 UIApplicationMain + 159<br/>
20  RealmTest1                          0x000000010691518f main + 111<br/>
21  libdyld.dylib                       0x000000010a44d68d start + 1<br/>

重新启动App后,内容已更改。 我该如何解决?

2 个答案:

答案 0 :(得分:1)

堆栈跟踪似乎与您发布的任何代码无关。问题是-[UIControl sendAction:to:forEvent:]正在调用一个没有实现该对象的对象的方法。这有两个正常原因:

  1. 您只需在注册触摸处理程序时传入错误的对象。
  2. 您注册为处理程序的对象已被释放,并且某些不相关的对象已在同一位置分配。在“方案 - &gt;运行 - &gt;诊断”中启用“僵尸对象”将使此案例以更具信息性的方式失败。

答案 1 :(得分:0)

尝试更改方法

static func updateTask(name:String, content:String, task: Task){
let taskId = task.id
let RealTask = realm.objects(Task.self).filter("id == %@", taskId).first
try! realm.write {
        RealTask?.name = name
        RealTask?.content = content
}
}

static func updateTask(name:String, content:String, task: Task){
let taskId = task.id
if let RealTask = realm.objects(Task.self).filter("id == %@", taskId).first {
 try! realm.write {
        RealTask.name = name
        RealTask.content = content
}}
}