Code after return will never be executed


(Christoph K) #1
func application(_ application: UIApplication, didFinishLaunchingWithOptions   
    launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        struct Platform {
            static let isSimulator: Bool = {
                #if arch(i386) || arch(x86_64)
                return true
               #endif
               return false
            }()
        }
//        window = UIWindow(frame: UIScreen.main.bounds)
//        window?.rootViewController = ViewController()
//        window?.makeKeyAndVisible()
    
    print("hello in application isSimulator=\(Platform.isSimulator)")
    // Override point for customization after application launch.
    if !Platform.isSimulator {
        print("running standalone")
    } else {
        print("running in simulator")
    }
    return true
}

Xcode IDE tells me that the code after return will never be executed.
Is it founded and if not, how can I get rid of that warning?


(t.ae) #2

Since return false appears even if the condition is true, the warning appears.
Why don't you use else?

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

(Christoph K) #3

I didn't read your post thoroughly, sorry :slight_smile:
Yes, that's the correct solution.


(Ben Rimmington) #4

You can also use #if targetEnvironment(simulator) from proposal SE-0190.