与雨燕Ç工作的API [英] Working with C APIs from Swift
问题描述
我试图跟踪的网络状态。我通过 FXReachability 的code去了。具体有以下方法
I'm trying to keep track of the network status. I went through the code of the FXReachability. Specifically the following method.
- (void)setHost:(NSString *)host
{
if (host != _host)
{
if (_reachability)
{
SCNetworkReachabilityUnscheduleFromRunLoop(_reachability, CFRunLoopGetMain(), kCFRunLoopCommonModes);
CFRelease(_reachability);
}
_host = [host copy];
_status = FXReachabilityStatusUnknown;
_reachability = SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, [_host UTF8String]);
SCNetworkReachabilityContext context = { 0, ( __bridge void *)self, NULL, NULL, NULL };
SCNetworkReachabilitySetCallback(_reachability, ONEReachabilityCallback, &context);
SCNetworkReachabilityScheduleWithRunLoop(_reachability, CFRunLoopGetMain(), kCFRunLoopCommonModes);
}
}
它所做的是它使检查到指定主机的连接。我想转换这种方法来斯威夫特和我有几个问题。
What it does is it keeps checking the connection to the specified host. I'm trying to convert this method to Swift and I'm having a couple of problems.
1。主机字符串转换为UTF8。
我要主机字符串转换为UTF8并把它传递到<一个href=\"https://developer.apple.com/library/ios/documentation/SystemConfiguration/Reference/SCNetworkReachabilityRef/#//apple_ref/c/func/SCNetworkReachabilityCreateWithName\"相对=nofollow> SCNetworkReachabilityCreateWithName 方法。我找不到在Objective-C UTF8字符串
属性的确切雨燕等同。但是有一个叫为
键入。根据本<一href=\"https://developer.apple.com/library/ios/documentation/swift/conceptual/Swift_Programming_Language/StringsAndCharacters.html\"相对=nofollow>文档, String属性UTF8
I have to convert the host string to UTF8 and pass it into the SCNetworkReachabilityCreateWithName method. I can't find the exact Swift equivalent of UTF8String
property in Objective-C. But there is a property called utf8
for the String
type. According to the documentation,
您可以通过遍历其UTF8属性访问字符串的UTF-8再presentation。此属性的类型String.UTF8View,这是8位无符号(UINT8)值的集合,一个用于在字符串的UTF-8再presentation每个字节的:
You can access a UTF-8 representation of a String by iterating over its utf8 property. This property is of type String.UTF8View, which is a collection of unsigned 8-bit (UInt8) values, one for each byte in the string’s UTF-8 representation:
我怎样才能得到一个字符串,而不是8位无符号的集合的完整UTF8重新presentation(UINT8)值?
How can I get a complete UTF8 representation of a string instead of a collection of unsigned 8-bit (UInt8) values?
2。回调函数<一个href=\"https://developer.apple.com/library/mac/documentation/SystemConfiguration/Reference/SCNetworkReachabilityRef/index.html#//apple_ref/c/func/SCNetworkReachabilitySetCallback\"相对=nofollow> SCNetworkReachabilitySetCallback 。
2. Callback function for SCNetworkReachabilitySetCallback.
TH这功能的第二个参数预计类型的东西 SCNetworkReachabilityCallBack
。我潜到源文件,并发现它实际上是一个 typealias
。
Th second parameter of this function expects something of type SCNetworkReachabilityCallBack
. I dived in to the source file and found out that it's actually a typealias
.
typealias SCNetworkReachabilityCallBack = CFunctionPointer<((SCNetworkReachability!, SCNetworkReachabilityFlags, UnsafeMutablePointer<Void>) -> Void)>
我定义它是这样的。
I defined it like this.
let reachabilityCallBack: SCNetworkReachabilityCallBack = {
}()
但我得到的错误的预期返回一个封闭缺少回报SCNetworkReachabilityCallBack时,在typealias你可以清楚地看到,它返回
的虚空。
But I get the error Missing return in a closure expected to return 'SCNetworkReachabilityCallBack' when in that typealias you can clearly see that it returns Void
.
这是错误的方式做到这一点?
Is this the wrong way to do this?
这是我面临的问题。我已经在这几个小时,现在没有运气,所以我会很AP preciate任何帮助。
These are the problems I'm facing. I've been at this for hours now with no luck so I'd really appreciate any help.
感谢您。
推荐答案
您第一个问题可以用
let reachability = host.withCString {
SCNetworkReachabilityCreateWithName(nil, $0).takeRetainedValue()
}
闭包中, $ 1,0
是一个指向的NUL结尾的UTF-8再presentation
字符串。
Inside the closure, $0
is a pointer to the NUL-terminated UTF-8 representation of the
String.
更新: 库克在说
现在一个答案被删除,也这里,
您可以在斯威夫特字符串实际上传递给函数取 UnsafePointer&LT;&UINT8 GT;
直接:
Update: As Nate Cook said in
a now deleted answer and also here,
you can actually pass a Swift string to a function taking a UnsafePointer<UInt8>
directly:
let reachability = SCNetworkReachabilityCreateWithName(nil, host).takeRetainedValue()
不幸的是,(据我所知)目前还没有解决您的第二个问题。 SCNetworkReachabilitySetCallback
需要一个指向一个C函数作为第二
参数,并且目前还没有通过一个夫特功能或封闭方法。
请注意,文档<一个href=\"https://developer.apple.com/library/mac/documentation/SystemConfiguration/Reference/SCNetworkReachabilityRef/index.html#//apple_ref/swift/tdef/SCNetworkReachabilityCallBack\"相对=nofollow> SCNetworkReachabilityCallBack
只显示了Objective-C的,但没有斯威夫特。
Unfortunately there is (as far as I know) currently no solution to your second problem.
SCNetworkReachabilitySetCallback
expects a pointer to a C function as second
parameter, and there is currently no method to pass a Swift function or closure.
Note that the documentation for SCNetworkReachabilityCallBack
shows only Objective-C but no Swift.
另请参阅斯威夫特是否与函数指针工作?。
对于斯威夫特2更新:现在可以通过一个封闭斯威夫特
采取一个函数指针参数的C函数。也 SCNetworkReachabilityCreateWithName()
不会再返回非托管对象:
Update for Swift 2: It is now possible to pass a Swift closure
to a C function taking a function pointer parameter. Also
SCNetworkReachabilityCreateWithName()
does not return an unmanaged object anymore:
let host = "google.com"
var context = SCNetworkReachabilityContext(version: 0, info: nil, retain: nil, release: nil, copyDescription: nil)
let reachability = SCNetworkReachabilityCreateWithName(nil, host)!
SCNetworkReachabilitySetCallback(reachability, { (_, flags, _) in
print(flags)
}, &context)
SCNetworkReachabilityScheduleWithRunLoop(reachability, CFRunLoopGetMain(), kCFRunLoopCommonModes)
这篇关于与雨燕Ç工作的API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!