#available 在区分watchOS和iOS时似乎不起作用.
以下是iOS和watchOS之间共享的代码示例:
lazy var session: WCSession = {
let session = WCSession.defaultSession()
session.delegate = self
return session
}()
Run Code Online (Sandbox Code Playgroud)
...
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 }
Run Code Online (Sandbox Code Playgroud)
似乎它只是默认为WatchOS,并且#available编译器不会考虑它.
我是否滥用此API或是否有其他方法可以区分iOS和WatchOS之间的代码?
更新:似乎我误用了BPCorp提到的API
使用Tali的上述代码解决方案:
#if os(iOS)
guard session.paired else { throw WatchBridgeError.NotPaired }
guard session.watchAppInstalled else { throw WatchBridgeError.NoWatchApp }
#endif …Run Code Online (Sandbox Code Playgroud) 我正在尝试通过JavaScriptCore加载外部库
具体来说,我想要最终的结果:
<script src="fancyLibrary.js" type="text/javascript"></script>
Run Code Online (Sandbox Code Playgroud)
但是使用以下语法:
JSContext *context = [[JSContext alloc] initWithVirtualMachine:[[JSVirtualMachine alloc] init]];
[context loadExternalJavascriptFileWithName:@"fancyLibrary.js"];
JSValue *fancyFunction = context[@"fancy"];
Run Code Online (Sandbox Code Playgroud)
这可能是在当前时间吗?我想我可以在应用程序包中包含外部库并从那里调用它.这是将库加载到上下文中的问题.