realm / realm-swift

Realm is a mobile database: a replacement for Core Data & SQLite
https://realm.io
Apache License 2.0
16.29k stars 2.14k forks source link

Why is this if returning false? #1211

Closed webmagnets closed 9 years ago

webmagnets commented 9 years ago

if var word: Word = Word.objectsWhere("simplified = '〇'").firstObject() as? Word

Why is this returning false when, as you can see in the attached image, there is an object with 〇 in the simplified column? ling

jpsim commented 9 years ago

This is normal Swift behavior (though arguably a language bug) and unrelated to Realm. You should read the section on Optional Binding of the Swift Programming Language Guide, if you haven't already.

Basically, .firstObject() as? Word fails because you're doing an optional cast rather than casting to an optional (.firstObject() as Word?). So I think your if statement should actually be if var word = Word.objectsWhere("simplified = '〇'").firstObject() as Word?. Note that I'm not specifying the type in word: Word because it's already specified as Word from optionally binding from Word?, so it's redundant.

Please reopen this issue if this is still causing you issues after making my suggested changes.

webmagnets commented 9 years ago

The real problem was I had "\r" at the end of the string in Word.simplified. Thanks for helping me rule the other stuff out.