Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's wrong with my #if TARGET_OS_SIMULATOR code for Realm path definition?

Tags:

swift

realm

I have this code

 #if TARGET_OS_SIMULATOR
let device = false
let RealmDB = try! Realm(path: "/Users/Admin/Desktop/realm/Realm.realm")
#else
let device = true
let RealmDB = try! Realm()
#endif

device bool works fine, yet RealmDB works only for else condition.

like image 803
alexey Avatar asked Mar 23 '16 14:03

alexey


1 Answers

As of Xcode 9.3+ Swift now supports #if targetEnvironment(simulator) to check if you're building for Simulator.

Please stop using architecture as a shortcut for simulator. Both macOS and the Simulator are x86_64 which might not be what you want.

// ObjC/C:
#if TARGET_OS_SIMULATOR
    // for sim only
#else
    // for device
#endif


// Swift:
#if targetEnvironment(simulator)
    // for sim only
#else
    // for device
#endif
like image 51
russbishop Avatar answered Oct 11 '22 22:10

russbishop