Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

New Xcode 4.3 compiler warnings - categories

BrianSlickBrianSlick Treadmill Desk NinjaPosts: 10,507Tutorial Authors, Registered Users @ @ @ @ @ @ @ @
edited May 2012 in iOS SDK Development
Just opened up a project in Xcode 4.3, and was instantly informed of all kinds of new warnings. Most of them were pretty harmless, like "void" on declaration but "IBAction" on implementation, easily fixed. But there is one that I can't figure out how to correct while still maintaining what I want.

Warning message is:
Category is implementing a method which will also be implemented by its primary class

It's pointing at methods in a category that are intended to be private, and are declared in a class extension within that category. It's also flagging some 3rd-party code, so I'm not the only one apparently doing this wrong.

A specific example is:

ActionValue
ActionValue+BTIAdditions

In ActionValue+BTIAdditions.m, I do this:
@interface ActionValue ()

- (NSString *)textValueFromDate;
- (NSString *)textValueFromPickerValues;

@end

These are private methods used within the category that I do not wish to be public. They are used by some of the public category methods.

The warning itself is placed down where I implement these methods. If you expand the warning to get to where the "original method" is defined, it highlights these lines here in the class extension.

The easy solution is to move these declarations to the .h file, and get rid of the class extension. But then these methods become public, which I don't want.

So, what is the correct way to have private methods in a category? My Googling is revealing what I was already doing.
Post edited by BrianSlick on
BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn - Professional iOS App Development. Available for hire.

SlickShopper 2 | Leave a PayPal donation

Free Xcode Tools: NSLog Utility | Getter Utility
GitHub Projects: BTIKit | BTICoreDataKit

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | A Model (Object) Is A Beautiful Thing
· ·

Replies

  • cribasoftcribasoft Posts: 159Registered Users
    edited February 2012
    I think Apple is trying to say "Don't override methods using categories." Or, more positively, "Categories are for adding/extending class behavior, not replacing."

    So I think you just want to say "Roger that, I'm going to do it anyway, thanks." and suppress the warning. I'm not in the habit of suppressing warnings but I'm sure there's docs out there about how to do that :)
    · ·
  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,507Tutorial Authors, Registered Users @ @ @ @ @ @ @ @
    edited February 2012
    That's the thing, though, I'm NOT replacing. In this particular case, the base class is an NSManagedObject subclass, with only the default properties and collections methods defined. Custom methods are defined in the category. There is no method in the base class being overridden. It appears to be flagged because I'm declaring the methods in the class extension ( in the category file), but they don't exist at all in the base class.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn - Professional iOS App Development. Available for hire.

    SlickShopper 2 | Leave a PayPal donation

    Free Xcode Tools: NSLog Utility | Getter Utility
    GitHub Projects: BTIKit | BTICoreDataKit

    Are you a newbie? Things you should read:
    Definitive Guide To Properties | UITableView Series | A Model (Object) Is A Beautiful Thing
    · ·
  • cribasoftcribasoft Posts: 159Registered Users
    edited February 2012
    BrianSlick wrote: »
    That's the thing, though, I'm NOT replacing. In this particular case, the base class is an NSManagedObject subclass, with only the default properties and collections methods defined. Custom methods are defined in the category. There is no method in the base class being overridden. It appears to be flagged because I'm declaring the methods in the class extension ( in the category file), but they don't exist at all in the base class.

    Oh, you're just using them to "hide" your methods... This sounds vaguely familiar from a WWDC vid I watched. Check out "Objective-C Advancements in Depth" from WWDC '11, right around 4:30. Maybe that helps? (sounds messy-ish to me)
    · ·
  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,507Tutorial Authors, Registered Users @ @ @ @ @ @ @ @
    edited February 2012
    cribasoft wrote: »
    Oh, you're just using them to "hide" your methods... This sounds vaguely familiar from a WWDC vid I watched. Check out "Objective-C Advancements in Depth" from WWDC '11, right around 4:30. Maybe that helps? (sounds messy-ish to me)

    Ok, I appreciate that you're trying to help, but you don't seem to understand that this is exactly what I am doing already. Look at the code I posted.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn - Professional iOS App Development. Available for hire.

    SlickShopper 2 | Leave a PayPal donation

    Free Xcode Tools: NSLog Utility | Getter Utility
    GitHub Projects: BTIKit | BTICoreDataKit

    Are you a newbie? Things you should read:
    Definitive Guide To Properties | UITableView Series | A Model (Object) Is A Beautiful Thing
    · ·
  • smithdale87smithdale87 Posts: 4,447iPhone Dev SDK Supporter @ @ @ @ @
    edited February 2012
    Are you OK with just disabling the compiler warning? Or are you looking more into cause & solution?
    · ·
  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,507Tutorial Authors, Registered Users @ @ @ @ @ @ @ @
    edited February 2012
    I can silence the compiler by moving the declarations to the .h file. What I would like to know is that if this technique is considered wrong, then what is the right way to do it?
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn - Professional iOS App Development. Available for hire.

    SlickShopper 2 | Leave a PayPal donation

    Free Xcode Tools: NSLog Utility | Getter Utility
    GitHub Projects: BTIKit | BTICoreDataKit

    Are you a newbie? Things you should read:
    Definitive Guide To Properties | UITableView Series | A Model (Object) Is A Beautiful Thing
    · ·
  • smithdale87smithdale87 Posts: 4,447iPhone Dev SDK Supporter @ @ @ @ @
    edited February 2012
    I don't have xcode 4.3 & Lion, so I can't try this.. but I'm not seeing any good explanations from Google.

    What if you give your category a name -
    @interface ActionValue (private)
    
    This would keep you from having to expose them as public methods at least.
    · ·
  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,507Tutorial Authors, Registered Users @ @ @ @ @ @ @ @
    edited February 2012
    [B]//  ActionValue+BTIAdditions.h[/B]
    
    #import "ActionValue.h"
    
    @interface ActionValue (ActionValue_BTIAdditions)
    
    // Public Method Declarations
    
    @end
    
    [B]//  ActionValue+BTIAdditions.m[/B]
    
    #import "ActionValue+BTIAdditions.h"
    
    [COLOR="Red"][B]@interface ActionValue ()
    
    // Private Method Declarations
    
    @end[/B]
    [/COLOR]
    @implementation ActionValue (ActionValue_BTIAdditions)
    
    // Public and Private Method Implementations
    
    @end
    

    The warning is related to the part in red.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn - Professional iOS App Development. Available for hire.

    SlickShopper 2 | Leave a PayPal donation

    Free Xcode Tools: NSLog Utility | Getter Utility
    GitHub Projects: BTIKit | BTICoreDataKit

    Are you a newbie? Things you should read:
    Definitive Guide To Properties | UITableView Series | A Model (Object) Is A Beautiful Thing
    · ·
  • cribasoftcribasoft Posts: 159Registered Users
    edited February 2012
    BrianSlick wrote: »
    [B]//  ActionValue+BTIAdditions.h[/B]
    
    #import "ActionValue.h"
    
    @interface ActionValue (ActionValue_BTIAdditions)
    
    // Public Method Declarations
    
    @end
    
    [B]//  ActionValue+BTIAdditions.m[/B]
    
    #import "ActionValue+BTIAdditions.h"
    
    [COLOR="Red"][B]@interface ActionValue ()
    
    // Private Method Declarations
    
    @end[/B]
    [/COLOR]
    @implementation ActionValue (ActionValue_BTIAdditions)
    
    // Public and Private Method Implementations
    
    @end
    

    The warning is related to the part in red.

    The video I posted said to put it in an internal header and import it in your category .m file. If it is in a header your warning goes away and since the .h will be an internal one you dont distribute they will be private, right?
    · ·
  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,507Tutorial Authors, Registered Users @ @ @ @ @ @ @ @
    edited February 2012
    I'm not sure where you got that in the video, but it sure isn't around the 4:30 mark. Whatever.

    I have now created:
    //  [B]ActionValue+Private.h[/B]
    
    #import "ActionValue.h"
    
    @interface ActionValue ()
    
    // Private Method Declarations
    
    @end
    

    Interestingly, this alone silenced the warning, but I'm not even #importing this file anywhere! And I didn't gain any warnings from where these methods are used. I even moved those implementations to the bottom of the file just to make sure. Clean build, etc. I don't see how it even knows that these methods exist.

    Creating this extra file seems silly. One more thing to manage. But I guess technically it solves the issue, so thanks for the tip.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn - Professional iOS App Development. Available for hire.

    SlickShopper 2 | Leave a PayPal donation

    Free Xcode Tools: NSLog Utility | Getter Utility
    GitHub Projects: BTIKit | BTICoreDataKit

    Are you a newbie? Things you should read:
    Definitive Guide To Properties | UITableView Series | A Model (Object) Is A Beautiful Thing
    · ·
  • vocarovocaro Posts: 15Registered Users
    edited May 2012
    What if you give your category a name -
    @interface ActionValue (private)
    

    I had the problem just as BrianSlick described it, and smithdale87's suggestion solved it for me. Instead of declaring the private methods with an empty category:
    @interface UIImage ()
    

    I supplied a unique category name, e.g.:
    @interface UIImage (MyPrivateMethods)
    

    The warning disappeared after that.
    · ·
Sign In or Register to comment.