#available似乎不工作时区分watchOS和iOS。
这里是一个代码在iOS& watchOS:
lazy var session: WCSession = { let session = WCSession.defaultSession() session.delegate = self return session }()
… …
if #available(iOS 9.0,*) { guard session.paired else { throw WatchBridgeError.NotPaired } // paired is not available guard session.watchAppInstalled else { throw WatchBridgeError.NoWatchApp } // watchAppInstalled is not available } guard session.reachable else { throw WatchBridgeError.NoConnection }
看来它只是默认为WatchOS和编译器不考虑#available。
我是滥用这个API,还是有其他方法来区分iOS和WatchOS之间的代码?
更新:似乎我是滥用API,如BPCorp所述
#if os(iOS) guard session.paired else { throw WatchBridgeError.NotPaired } guard session.watchAppInstalled else { throw WatchBridgeError.NoWatchApp } #endif guard session.reachable else { throw WatchBridgeError.NoConnection }
不幸的是没有#if os(watchOS).. as of Xcode 7 GM
编辑:不知道什么时候添加,但你现在可以做#if os(watchOS)在Xcode 7.2