Search code examples
swiftmagicalrecordfindfirst

Swift 3 - MagicalRecord : How use findFirst on empty entitie


I call the MagicalRecord's findFirst() method on an entity which is empty on the first run.

I have the error

EXC_BAD_INSTRUCTION (code=EXC_i386_INVOP...),

and if I insert one line before calling findFirst() method, no error (or if I call findAll() with empty entity, no error too)

So is there a method to call properly findFirst optional ? Swift enforce the !

let user: User? = User.mr_findFirst()! as User?

Solution

  • I missed something when I was trying all possibilities... But after came back to basics, I found the solution :

    if let user = User.mr_findFirst() {   }
    

    Thanks