Swift 3 UnsafePointer($0) no longer compile in Xcode 8 beta 6

后端 未结 3 2140
北恋
北恋 2020-12-13 23:24

My code snipet as follows …:

    let defaultRouteReachability = withUnsafePointer(to: &zeroAddress) {
        SCNetworkReachabilityCreateWithAddress(nil,         


        
3条回答
  •  悲&欢浪女
    2020-12-13 23:44

    From the Release Notes of Xcode 8 beta 6:

    • An Unsafe[Mutable]RawPointer type has been introduced, replacing Unsafe[Mutable]Pointer. Conversion from UnsafePointer to UnsafePointer has been disallowed. Unsafe[Mutable]RawPointer provides an API for untyped memory access, and an API for binding memory to a type. Binding memory allows for safe conversion between pointer types. See bindMemory(to:capacity:), assumingMemoryBound(to:), and withMemoryRebound(to:capacity:). (SE-0107)

    In your case, you may need to write something like this:

    let defaultRouteReachability = withUnsafePointer(to: &zeroAddress) {
        $0.withMemoryRebound(to: sockaddr.self, capacity: 1) {zeroSockAddress in
            SCNetworkReachabilityCreateWithAddress(nil, zeroSockAddress)
        }
    }
    

提交回复
热议问题