After a lot of troubles, i finally got my code converted to Swift 3.0.
But it seems like my incrementID function isn't working anymore?
Any suggestions how i can fix this?
My incrementID and primaryKey function as they look right now.
override static func primaryKey() -> String? {
return "id"
}
func incrementID() -> Int{
let realm = try! Realm()
let RetNext: NSArray = Array(realm.objects(Exercise.self).sorted(byProperty: "id")) as NSArray
let last = RetNext.lastObject
if RetNext.count > 0 {
let valor = (last as AnyObject).value(forKey: "id") as? Int
return valor! + 1
} else {
return 1
}
}
There's no need to use KVC here, or to create a sorted array just to get the max value. You can just do:
func incrementID() -> Int {
let realm = try! Realm()
return (realm.objects(Exercise.self).max(ofProperty: "id") as Int? ?? 0) + 1
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With