[Swift4] 프로토콜에서 웹선언 무시 의혹.
protocol HogeView: class {
weak var delegate: HogeViewDelegate? { get set }
}
protocol HogeViewDelegate: class {
// delegate functions
}
class HogeViewController: UIViewController, HogeView {
weak var delegate: HogeViewDelegate?
// 以下VC実装
}
하지만 이 항목은 Xcode 9입니다.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
로 바뀌지 않았습니다...?그렇게 생각하면 스위프트 4.1
protocol
선언하지 못해도 동작에 변화가 없다, 아이구... 추기 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
weak
에 출연하는 기간에만 리턴(범위를 제외하면release)이 되기 때문에 순환 참조 문제는 문제라고 볼 수 있을까요?
Reference
이 문제에 관하여([Swift4] 프로토콜에서 웹선언 무시 의혹.), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/417_72ki/items/7c07beeeac571b7121cd
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
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
Reference
이 문제에 관하여([Swift4] 프로토콜에서 웹선언 무시 의혹.), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/417_72ki/items/7c07beeeac571b7121cd텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)