简体   繁体   English

Swift:你如何使用链接库中的Objective-C函数?

[英]Swift: How do you use an Objective-C function from a linked library?

Background 背景

I'm new to iOS development and playing with Swift for the sake of learning. 我是iOS开发的新手,为了学习而玩Swift。 As a small challenge, I'm trying to get the SSID of network to which a device is currently connected. 作为一个小挑战,我正在尝试获取设备当前连接的网络的SSID。

This is well-covered ground on Stack using Objective-C: iPhone get SSID without private library , for example ... but the Objective-C / Swift dance is causing me some conceptual challenges. 这是使用Objective-C在Stack上完全覆盖的基础: iPhone获得没有私有库的SSID ,例如...但Objective-C / Swift舞蹈给我带来了一些概念上的挑战。

Specifically, the suggested solution (discussed in the above-linked post) is to call the function CNCopyCurrentNetworkInfo() -- but, according to Apple's documentation , this function is not available in Swift. 具体来说,建议的解决方案(在上面链接的帖子中讨论)是调用函数CNCopyCurrentNetworkInfo() - 但是,根据Apple的文档 ,这个函数在Swift中不可用。

So far 至今

I've included (I think correctly) SystemConfiguration.framework by adding it to the Linked Frameworks and Libraries of the project. 我已将(我认为正确) SystemConfiguration.framework包含在项目的Linked Frameworks和Libraries中。 I've also brought it into the View Controller using import SystemConfiguration . 我还使用import SystemConfiguration将它带入View Controller。

Questions 问题

  1. Do I need to also use import SystemConfiguration , or is that already done due to including the framework with my project? 我是否需要使用import SystemConfiguration ,或者由于将框架包含在我的项目中而已经完成了?
  2. Is there another / better way to include the SystemConfiguration library? 是否有另一种/更好的方法来包含SystemConfiguration库?

  3. The big one : How, once the required library is imported, do you call the Objective-C function from the library? 最重要的一点 :如果导入了所需的库,您是否从库中调用Objective-C函数?

Thank you! 谢谢!

I know this is not directly answering your question, but it's an answer to your problem. 我知道这不是直接回答你的问题,但它是你问题的答案。 You can do everything in swift in your case. 在你的情况下,你可以迅速做一切。

After importing the library, 导入库后,

import SystemConfiguration.CaptiveNetwork

You can immediately call: 您可以立即致电:

CNCopySupportedInterfaces()

and it will work. 它会起作用。 Confirmed in Xcode 6.3.2. 在Xcode 6.3.2中确认。

Apple's interoperability book (from Understanding the Swift Import Process ): Apple的互操作性书(来自了解Swift导入过程 ):

Any Objective-C framework (or C library) that's accessible as a module can be imported directly into Swift. 任何可作为模块访问的Objective-C框架(或C库)都可以直接导入Swift。 This includes all of the Objective-C system frameworks—such as Foundation, UIKit, and SpriteKit—as well as common C libraries supplied with the system. 这包括所有Objective-C系统框架 - 例如Foundation,UIKit和SpriteKit - 以及随系统提供的通用C库。

Regarding your specific questions: 关于您的具体问题:

  1. The opposite is the case: you do not need to manually include Apple's frameworks. 情况恰恰相反:您不需要手动包含Apple的框架。 Xcode will automatically make them available to you given a valid import statement (such as import SystemConfiguration in your case), even – or rather: especially – in a playground! 给定有效的import语句(例如在您的情况下import SystemConfiguration ),Xcode会自动为您提供它们,甚至 - 或者更确切地说 - 在操场上!

  2. ditto... 同上...

  3. Given the above import statement, SystemConfiguration is indeed imported since you can call its functions (eg SCCopyLastError() // --> __NSCFError ) and access its constants (eg kCFErrorDomainSystemConfiguration // --> com.apple.SystemConfiguration ). 鉴于上面的import语句,确实导入了SystemConfiguration因为你可以调用它的函数(例如SCCopyLastError() // --> __NSCFError )并访问它的常量(例如kCFErrorDomainSystemConfiguration // --> com.apple.SystemConfiguration )。 Unfortunately, CaptiveNetwork does not seem to be imported with it (eg CNCopySupportedInterfaces() // --> Use of unresolved identifier ). 不幸的是, CaptiveNetwork似乎没有随它导入(例如CNCopySupportedInterfaces() // --> Use of unresolved identifier )。

You should be able, however, to use this framework on the Objective C side and simply call your own wrapper functions from Swift. 但是,您应该能够在Objective C端使用此框架,并从Swift中调用您自己的包装器函数。 You just need to remember to include them among the imports listed in your bridging header (see Swift and Objective-C in the Same Project for more about bridging headers). 您只需要记住将它们包含在桥接头中列出的导入中(有关桥接头的更多信息,请参阅同一项目中的Swift和Objective-C )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM