2016-03-23 9 views

Odpowiedz

11

TARGET_IPHONE_SIMULATOR makro nie działa w Swift. Co chcesz zrobić, to jak poniżej, prawda?

#if arch(i386) || arch(x86_64) 
let device = false 
let RealmDB = try! Realm(path: "/Users/Admin/Desktop/realm/Realm.realm") 
#else 
let device = true 
let RealmDB = try! Realm() 
#endif 
+0

teraz działa! Dziękuję Ci bardzo! – alexey

+0

Proszę nie używać architektury jako skrótu do symulatora. – russbishop

0

Więcej informacji na temat tego problemu to hear. Używam this podejście:

struct Platform { 
     static let isSimulator: Bool = { 
      var isSim = false 
      #if arch(i386) || arch(x86_64) 
       isSim = true 
      #endif 
      return isSim 
     }() 
    } 

    // Elsewhere... 

    if Platform.isSimulator { 
     // Do one thing 
    } 
    else { 
     // Do the other 
    } 

Albo tworzyć klasa util

class SimulatorUtility 
{ 

    class var isRunningSimulator: Bool 
    { 
     get 
     { 
      return TARGET_OS_SIMULATOR != 0// for Xcode 7 
     } 
    } 
} 
+0

dziękuję za odpowiedź! chodzi o to, że mogę umieścić ten "jeśli Platform.isSimulator" wewnątrz jakiejś klasy, ale muszę ustawić tę stałą "niech RealmDB ..." na najwyższym poziomie zaraz po "import RealmSwift". – alexey

+0

NIE używaj architektury jako skrótu do symulatora. – russbishop

3

Zobacz ten post. Jest to poprawny sposób to zrobić i jest to dobrze wyjaśnione

https://samsymons.com/blog/detecting-simulator-builds-in-swift/

Zasadniczo zdefiniować zmienną o nazwie jak chcesz (może 'symulatora) należy ustawić podczas biegu w symulatorze. Ustaw w Ustawieniach kompilacji celu, pod Active Compilation Conditions ->Debug następnie (+) następnie wybierz Any iOS Simulator SDK z listy rozwijanej, a następnie dodaj zmienną.

Następnie w kodzie

var isSimulated = false 
#if SIMULATOR 
    isSimulated = true // or your code 
#endif 
0

As z Xcode 9.3+ Swift obsługuje teraz if #targetEnvironment(simulator) aby sprawdzić, czy jesteś budynku dla Simulator.

Proszę przestać używać architektury jako skrótu do symulatora.

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


// Swift: 
#if targetEnvironment(simulator) 
    // for sim only 
#else 
    // for device 
#endif 
Powiązane problemy