Reputation: 11
I am making a simple list app with Realm and I have trouble making update method. This is my code.
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
}
}
error message.
2017-02-10 17:13:57.179 RealmTest1[7156:9710210] -[RealmTest1.EditViewController saveTask:]: unrecognized selector sent to instance 0x7fd06351eaa0 2017-02-10 17:13:57.183 RealmTest1[7156:9710210] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[RealmTest1.EditViewController saveTask:]: unrecognized selector sent to instance 0x7fd06351eaa0'
stack trace
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/>
After I restart App, content is already changed. How can I fix it?
Upvotes: 1
Views: 792
Reputation: 8138
The stack trace appears to be unrelated to any of the code you've posted. The problem is that -[UIControl sendAction:to:forEvent:]
is calling a method on an object which does not implement that object. There are two normal causes for this:
Upvotes: 1
Reputation: 420
Try to change your method
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
}
}
to
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
}}
}
Upvotes: 0