Swift 3 - CFHostScheduleWithRunLoop 崩溃 [英] Swift 3 - CFHostScheduleWithRunLoop crash

查看:33
本文介绍了Swift 3 - CFHostScheduleWithRunLoop 崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Swift 中执行反向 DNS,我之前在 Swift 2.2 上的代码运行良好,而且我已经在 Objective-C 中实现了它并且可以正常工作.但是我不能让它在 Swift 3.0 中工作

I am doing a reverse DNS in Swift, my previous code on Swift 2.2 was working fine, also I have implemented it in Objective-C and it works to. However I am not able to make it works in Swift 3.0

斯威夫特 2.2

//: Let's set up the `sockaddr_in` C structure using the initializer.
var sin = sockaddr_in(
    sin_len: UInt8(sizeof(sockaddr_in)),
    sin_family: sa_family_t(AF_INET),
    sin_port: in_port_t(0),
    sin_addr: in_addr(s_addr: inet_addr(ip)),
    sin_zero: (0,0,0,0,0,0,0,0)
)

//: Now convert the structure into a `CFData` object.
let data = withUnsafePointer(&sin) { ptr in
    CFDataCreate(kCFAllocatorDefault, UnsafePointer(ptr), sizeof(sockaddr_in))
}

//: Create the `CFHostRef` with the `CFData` object and store the retained value for later use.
let host = CFHostCreateWithAddress(kCFAllocatorDefault, data).takeRetainedValue()

//: Now schedule the runloop for the host.
CFHostScheduleWithRunLoop(host!, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode)

var error = CFStreamError()

//: Start the info resolution.
CFHostStartInfoResolution(host!, .Names, &error)

斯威夫特 3.0

//: Let's set up the `sockaddr_in` C structure using the initializer.
var sin = sockaddr_in(
    sin_len: UInt8(sizeof(sockaddr_in)),
    sin_family: sa_family_t(AF_INET),
    sin_port: in_port_t(0),
    sin_addr: in_addr(s_addr: inet_addr(ip)),
    sin_zero: (0,0,0,0,0,0,0,0)
)

//: Now convert the structure into a `CFData` object.
let data = NSData(bytes: &sin, length: MemoryLayout<sockaddr_in>.size) as CFData

//: Create the `CFHostRef` with the `CFData` object and store the retained value for later use.
let host = CFHostCreateWithAddress(kCFAllocatorDefault, data).takeRetainedValue()

//: Now schedule the runloop for the host.
CFHostScheduleWithRunLoop(host!, CFRunLoopGetCurrent(), CFRunLoopMode.defaultMode as! CFString)

var error = CFStreamError()

//: Start the info resolution.
CFHostStartInfoResolution(host!, .Names, &error)

当我运行这段代码时它崩溃了

When I ran this code it it crash at

CFHostScheduleWithRunLoop

CFHostScheduleWithRunLoop

有什么想法吗?

推荐答案

尝试替换:

CFRunLoopMode.defaultMode as! CFString

与:

CFRunLoopMode.defaultMode!.rawValue

这篇关于Swift 3 - CFHostScheduleWithRunLoop 崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆