Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Realm error: Invalid Value, expecting int and receiving: 0

Tags:

ios

swift

realm

I am using Realm with Swift for a query, but am receiving this error:

Terminating app due to uncaught exception 'Invalid value', reason: 'Expected object of type int for property 'id' on object of type 'JournalEntryLine', but received: 0'

The JournalEntryLine class does have a property (Int) named id.

The code I'm using:

for item in idSet
    let idQuery = realm.objects(JournalEntryLine).filter("id = '\(item)' AND type = 'Debit'")
}

idSet is a set containing integers 0 and onward. I have confirmed that at least [0] is always in the set before running this query.

Why am I getting this error?

like image 277
Jared Avatar asked Jun 09 '15 06:06

Jared


2 Answers

-EDIT-

  • If you use: id = 'YOUR_VAR_OR VALUE' => means that id is a String (Ex: id ='4')

  • But if you use: id = YOUR_VAR_OR VALUE => means that id is an integer (Ex: id = 4)

NB: So when your id is an integer don't use quotes ' '


Try this:

let idQuery = realm.objects(JournalEntryLine).filter("id = \(item) AND type = 'Debit'")

Transform id = '\(item)' to id = \(item) because id is an Integer, if you use quotes, il will consider id as a string.

Don't forget to vote Up if it helps you. :)

like image 90
Masterfego Avatar answered Nov 06 '22 05:11

Masterfego


Also it's helped to me:

let matchedMovieID = realm.objects(MovieID.self).filter("movieID == %@", idString).first

Swift 4.2, XCode 10.0, Realm 3.14.1

like image 2
Zhanserik Avatar answered Nov 06 '22 04:11

Zhanserik