Xcode 8 says “Do you want to add a stub?” How do I answer?

后端 未结 3 1854
一整个雨季
一整个雨季 2021-01-18 04:49

In Xcode 8, if you declare that a class (or struct) conforms to a protocol, but haven\'t implemented the required methods (functions?) Xcode will give this message \"Type \'

相关标签:
3条回答
  • 2021-01-18 05:19

    Yes, click 'fix it', xcode will add neccessary methods and variables as mentioned in your protocol, and one more thing, Xcode will add variables with proper read write permission in the class implementing the protocol.

    For example: in your protocol if you declare a get,set variable and in your struct/class you are declaring it as 'let' property, then Xcode will throw a error saying "Do you want to add a protocol stub", when you click fix it , it now add 'var' property in protocol abiding class/struct for a get,set property in protocol

    protocol VoiceAssistant {
      var name: String {get}
      var voice: String {get set}
    }
    
    struct Siri: VoiceAssistant {
    
     var voice: String //xcode added this, when you click 'fix it' for protocol stub
     let name = "Siri"   
    
     let voice = "Voice" //added by me, Compilation Error: voice is not settable, but protocol requires it.
    }
    
    0 讨论(0)
  • 2021-01-18 05:40

    The answer of raphh is right but xcode is still a little buggy here (at least for me),

    If you don't select the Fix-it right after compiling the dot mark transform to exclamation mark and you can't make it appear again unless you try to build one more time.

    Right after building : Dot error icon

    When you don't do the Fix-it right away : exclamation mark error icon

    You have to re-build to get the dot icon again when you have several methods to implement and you did do the Fix-it for the first one.

    0 讨论(0)
  • 2021-01-18 05:42

    Simply click on Fix-it and Xcode will add you the stub for that method you need to implement.

    Like this. See :

    Thank you Xcode 8, finally !

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