Swift 3 UnsafePointer($0) 不再在 Xcode 8 beta 6 中编译 [英] Swift 3 UnsafePointer($0) no longer compile in Xcode 8 beta 6
本文介绍了Swift 3 UnsafePointer($0) 不再在 Xcode 8 beta 6 中编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的代码片段如下...:
My code snipet as follows …:
let defaultRouteReachability = withUnsafePointer(to: &zeroAddress) {
SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer($0))
}
... 不再编译,出现以下我不明白的错误:
… does no longer compile with the following error which I don't understand:
"'init' is unavailable: use 'withMemoryRebound(to:capacity:_)' to temporarily view memory as another layout-compatible type."
如何修复它?
推荐答案
来自 Xcode 8 beta 6 的发行说明:
From the Release Notes of Xcode 8 beta 6:
- 引入了
Unsafe[Mutable]RawPointer
类型,替代了Unsafe[Mutable]Pointer
.从UnsafePointer
到UnsafePointer
已被禁止.Unsafe[Mutable]RawPointer
提供用于无类型内存访问的 API 和用于绑定的 API记忆到一个类型.绑定内存允许在指针类型.见bindMemory(to:capacity:)
,assumingMemoryBound(to:)
,和withMemoryRebound(to:capacity:)
.(SE-0107)
- An
Unsafe[Mutable]RawPointer
type has been introduced, replacingUnsafe[Mutable]Pointer<Void>
. Conversion fromUnsafePointer<T>
toUnsafePointer<U>
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. SeebindMemory(to:capacity:)
,assumingMemoryBound(to:)
, andwithMemoryRebound(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)
}
}
这篇关于Swift 3 UnsafePointer($0) 不再在 Xcode 8 beta 6 中编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文