4 Replies
      Latest reply: Jan 30, 2017 2:20 PM by QuinceyMorris RSS
      applegg Level 1 Level 1 (0 points)

        One of Objective C's features was being loosly typed. You could send any message to any id by testing "respondsToSelector". Now, it seems that warning messages appear for delegates which are not defined by a protocol. So, is it the intert to force using protocols for every delegate? We've got a lot of legacy code for which I really hate creating potocols to silence these warnings.

         

        I notice that collection classes now seem to want a type specified for what is collected. That's probably a good thing and consistent with the miigration to Swift.

        • Re: Delegate types
          QuinceyMorris Level 7 Level 7 (4,120 points)

          Can you give some specific examples? (declaration, use, and the exact error message)

           

          In general, the reverse change seems to be happening: delegate properties that were just id have become id<SomeProtocol>. AFAIK the ability to send an arbitrary message to a genuine id variable hasn't changed.

            • Re: Delegate types
              applegg Level 1 Level 1 (0 points)

              This may be part of my on-going saga with XCode 8.2.1 with my multi-target iOS project -- claimed missing files, etc. However, I am getting warnings on both lines for things like:

               

                if([self.delegate respondsToSelector:@selector(handleColorButton:)])

                [self.delegate performSelector:@selector(handleColorButton:) withObject:button];

               

              saying: "Undeclared selector 'handleColorButton:"' The delegate does implement that method.

                • Re: Delegate types
                  QuinceyMorris Level 7 Level 7 (4,120 points)

                  In the absence of more context (about what was included) then I would guess that the problem is nothing to do with the delegate as such, but rather that the compiler simply has never seen any definition of "handleColorButton:"  — or, if you're Quinn, "varnishWaffles".

                   

                  This could indeed be a consequence of header files not being included. Or it could be a consequence of one of the build settings for complaining about undeclared selectors having been turned on when your project got updated for Xcode 8.2.1, when it was previously off or didn't exist yet.

              • Re: Delegate types
                eskimo Apple Staff Apple Staff (7,530 points)

                Now, it seems that warning messages appear for delegates which are not defined by a protocol.

                I’m going to second QuinceyMorris’s request for some examples here.  However, there is one specific issue you have to watch out for here, namely ARC.  Assuming a delegate like this:

                @property (nonatomic, weak, readwrite) id delegate;
                

                then the following code will fail because the compiler in ARC mode needs to know the type signature of the method, and it’s never seen that method before.

                [self.delegate varnishWaffles];
                

                The best fix for this is to make the delegate confirm to a protocol and add the method there.  But you can avoid the problem by declaring the method anyway that the compiler can see it.

                Share and Enjoy

                Quinn “The Eskimo!”
                Apple Developer Relations, Developer Technical Support, Core OS/Hardware
                let myEmail = "eskimo" + "1" + "@apple.com"