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

10k Views Asked by At

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?

2

There are 2 best solutions below

0
On BEST ANSWER

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

0
On

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