[Swift4] protocolでweak宣言が無視されている疑惑
Swift4.0でこんな実装をしていた。
protocol HogeView: class {
weak var delegate: HogeViewDelegate? { get set }
}
protocol HogeViewDelegate: class {
// delegate functions
}
class HogeViewController: UIViewController, HogeView {
weak var delegate: HogeViewDelegate?
// 以下VC実装
}
ところが、このプロジェクトをXcode9.4.1で開いたところ
'weak' should not be applied to a property declaration in a protocol and will be disallowed in future versions
という警告が出るように
調べてみると、
[SE-0186] Remove ownership keyword support in protocols
ということでprotocol
でweak
なプロパティが宣言できなくなる模様。
そこでちょっと気になったので
Swift4.0でこんなコードを書いてみた。
//: Playground - noun: a place where people can play
import Foundation
class A {}
extension A: CustomDebugStringConvertible {
var debugDescription: String {
return "A(\(Unmanaged.passUnretained(self).toOpaque()))"
}
}
protocol P: class {
weak var weakVar: A? { get set }
}
class B: P {
weak var weakVar: A?
}
extension B: CustomDebugStringConvertible {
var debugDescription: String {
return "B(\(Unmanaged.passUnretained(self).toOpaque()))"
}
}
var b: B = B()
print(b, b.weakVar.debugDescription)
b.weakVar = A()
print(b, b.weakVar.debugDescription)
print()
var p: P = B()
print(p, p.weakVar.debugDescription)
p.weakVar = A()
print(p, p.weakVar.debugDescription)
結果
B(0x0000600000030600) nil
B(0x0000600000030600) nil
B(0x00006000000335c0) nil
B(0x00006000000335c0) Optional(A(0x0000600000008530)) <- !?
P
型で宣言した変数のweakVar
がweak
になっていない...?
そう考えるとSwift4.1でprotocol
でweak
宣言できなくなっても動きとしては変わらなそうだがはて...
追記
class A {
deinit {
print("deinit A")
}
}
// 中略
var b: B = B()
print(b, b.weakVar.debugDescription)
b.weakVar = A()
print(b, b.weakVar.debugDescription)
print()
var obj: Any = b
if let p = obj as? P {
print(p, p.weakVar.debugDescription)
p.weakVar = A()
print(p, p.weakVar.debugDescription)
}
print(b, b.weakVar.debugDescription)
class A {
deinit {
print("deinit A")
}
}
// 中略
var b: B = B()
print(b, b.weakVar.debugDescription)
b.weakVar = A()
print(b, b.weakVar.debugDescription)
print()
var obj: Any = b
if let p = obj as? P {
print(p, p.weakVar.debugDescription)
p.weakVar = A()
print(p, p.weakVar.debugDescription)
}
print(b, b.weakVar.debugDescription)
結果
B(0x00006000000273e0) nil
deinit A
B(0x00006000000273e0) nil
B(0x00006000000273e0) nil
B(0x00006000000273e0) Optional(A(0x0000604000003f10))
deinit A
B(0x00006000000273e0) nil
protocol
でキャストしている間だけretainされている(スコープ抜けたらreleaseされてる)ようなので、循環参照問題は大丈夫と見ていいのかなー
Author And Source
この問題について([Swift4] protocolでweak宣言が無視されている疑惑), 我々は、より多くの情報をここで見つけました https://qiita.com/417_72ki/items/7c07beeeac571b7121cd著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .