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

Jared picture Jared · Jun 9, 2015 · Viewed 7.8k times · Source

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?

Answer

Masterfego picture Masterfego · Jun 9, 2015

-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. :)