2016-09-13 73 views
0

我在Swift中做了一个反向DNS,我在Swift 2.2上的前面的代码工作正常,我也在Objective-C中实现了它,它可以工作。但是我不能让它工作在雨燕3.0Swift 3 - CFHostScheduleWithRunLoop crash

雨燕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) 

当我运行这段代码它,它坠毁在

CFHostScheduleWithRunLoop

有什么想法?

+0

你的雨燕3.0的代码不会在Xcode 8(8A218a)编译。你是如何运行的? – OOPer

回答

1

尝试更换:

CFRunLoopMode.defaultMode as! CFString 

有:

CFRunLoopMode.defaultMode!.rawValue