[Swift4] 프로토콜에서 웹선언 무시 의혹.

9558 단어 SwiftSwift4.1
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実装
}
하지만 이 항목은 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
따라서 protocolweak의 속성을 발표할 수 없습니다.
거기서 조금 신경이 쓰여요.
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유형성명의 변수weakVarweak로 바뀌지 않았습니다...?
그렇게 생각하면 스위프트 4.1protocol 선언하지 못해도 동작에 변화가 없다, 아이구...

추기

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)이 되기 때문에 순환 참조 문제는 문제라고 볼 수 있을까요?

좋은 웹페이지 즐겨찾기