protocol Property {}
protocol OptionalProtpery : Property {
static func codeNilInto(pointer: UnsafePointer<Int>)
}
extension Optional : OptionalProtpery {
static func codeNilInto(pointer: UnsafePointer<Int>) {
(UnsafeMutablePointer(pointer) as UnsafeMutablePointer<Optional>).memory = nil
}
}
我对此声明UnsafeMutablePointer<"Optional">
感兴趣
例如,为什么我们没有错误:&#34;可选的泛型类型并且需要参数&lt; ...&gt;&#34;。由于此处未指定特定类型(例如UnsafeMutablePointer&lt;&#34; Optional&#34;&lt;&#34; Int&#34;&gt;&gt;&gt;)。
答案 0 :(得分:0)
protocol Property {}
protocol OptionalProtpery : Property {
static func codeNilInto(pointer: UnsafePointer<Int>)
}
extension Optional : OptionalProtpery {
static func codeNilInto(pointer: UnsafePointer<Int>) {
(UnsafeMutablePointer(pointer) as UnsafeMutablePointer<Optional>).memory = nil
}
}
var i = 1
let p = withUnsafePointer(&i) { (p) -> UnsafePointer<Int> in
return p
}
print(p.memory) // 1
//Optional.codeNilInto(p) // error: generic parameter 'Wrapped' could not be inferred
// you have to specify generic parameter !!!
Optional<Int>.codeNilInto(p)
Optional<Double>.codeNilIntro(p)
// or what ever you want to express
所以,因为Optional是通用的,所以你的代码是'OK'。顺便说一下,您对代码的期望是什么?