Why can the keyword “weak” only be applied to class and class-bound protocol types

后端 未结 11 1299
清酒与你
清酒与你 2020-12-13 03:14

When I\'m declaring variables as weak in Swift, I sometimes get the error message from Xcode:

\'weak\' may only be applied to class and

相关标签:
11条回答
  • 2020-12-13 03:51
    1. weak is not for value type.
    2. weak comes to the picture only for the class.

    "weak" can apply anything which is inherited from class or class-bound protocol types

    1. Class Protocol: protocol ViewControllerDelegate : class { func getInformationk(value: String?) }
    2. NSObjectProtocol:

      protocol ViewControllerDelegate : NSObjectProtocol { func getInformation(value: String?) }

    0 讨论(0)
  • 2020-12-13 03:52

    Well just in case anyone else thinks that you have everything correct in your code like me, check that you did not mistakenly replaced the : by an =.

    Here is what I had. It was also giving me the same error as above:

    protocol PenguinDelegate: class {
        func userDidTapThePenguin()
    }
    
    class MyViewController: UIViewController {
        weak var delegate = PenguinDelegate?
    }
    

    But the correct way is:

    protocol PenguinDelegate: class {
        func userDidTapThePenguin()
    }
    
    class MyViewController: UIViewController {
        weak var delegate: PenguinDelegate?
    }
    

    Do you see the difference? It took me a while to see that I had an equal sign instead of a colon. Also note that I did get other errors for the same line for I had decided my first error seem like the most likely to be the real problem :

    -weak may only be applied to class and class-bound protocol types

    :-<

    0 讨论(0)
  • 2020-12-13 03:53

    weak only works for reference type, so Xcode would report an error if you are calling from struct (instead of class).

    0 讨论(0)
  • 2020-12-13 03:56

    weak is a qualifier for reference types (as opposed to value types, such as structs and built-in value types).

    Reference types let you have multiple references to the same object. The object gets deallocated when the last strong reference stops referencing it (weak references do not count).

    Value types, on the other hand, are assigned by copy. Reference counting does not apply, so weak modifier does not make sense with them.

    0 讨论(0)
  • 2020-12-13 03:58

    I find out in one case where you even have class type but still you get this error message.

    For example,

    class MyVC: UIViewController {
       var myText: UITextView = {
          [weak self]
          let text = UITextView()
          // some codes using self
          return text
       }()
    }
    

    Here an UITextView object is returned from an anonymous block as initialization of var myText. I got the same type of error message. To resolve the issue, the var has to be marked as lazy:

    class MyVC: UIViewController {
       lasy var myText: UITextView = {
          [weak self]
          let text = UITextView()
          // some codes using self
          return text
       }()
    }
    
    0 讨论(0)
  • 2020-12-13 03:59

    weak is for ARC(Automatic Reference Counting). It means not adding reference count. So it only works for Class. And in Swift, you will get optional value for security.

    0 讨论(0)
提交回复
热议问题