2016-09-13 8 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 코드는 엑스 코드 8 (8A218a)에 컴파일되지 않습니다. 어떻게 달아 봤어? – OOPer

답변

1

봅니다 대체 :

CFRunLoopMode.defaultMode as! CFString 

로 :

CFRunLoopMode.defaultMode!.rawValue