0

When creating a class, a BOOL property declaration often looks like this:

@property (nonatomic, getter=isActive) BOOL active;

For normal classes and subclasses, naming the getter is<#Key#> makes sense, looks sharp, and is nice to use in if statements.

What about when naming properties in a category though?

I was just implementing some helper methods on NSView, when all of the sudden, things took a hideous turn:

@property (nonatomic, readonly, getter=bsd_isFieldEditorDelegate) BOOL bsd_fieldEditorDelegate;

After adding the recommended prefix (bsd_ in my case), the semantic value of the getter variation seems to get lost. In this case, should I just remove the getter name altogether or what?

I looked throughout the documentation, but I couldn't find a definitive answer. Is there one, or am I just overthinking it?

Ben Stock
  • 1,986
  • 1
  • 22
  • 30

1 Answers1

0

You CANNOT use property in Category of Objective-C. Instead of, you should use Associated object of Objective-C Runtime. Because of that, attribute of property is may not work with Associated object.

Reference: Objective-C: Property / instance variable in category

Community
  • 1
  • 1
Huy Le
  • 2,503
  • 1
  • 15
  • 15
  • Thanks for the link. However, according to Apple's documentation, “It’s valid syntax to include a property declaration in a category interface, but it’s not possible to declare an additional instance variable in a category. This means the compiler won’t synthesize any instance variable, nor will it synthesize any property accessor methods. You can write your own accessor methods in the category implementation, but you won’t be able to keep track of a value for that property unless it’s already stored by the original class.” If you notice above, I've declared the property `readonly`. – Ben Stock Feb 24 '15 at 06:05
  • Continued from above comment: I'm not tracking any state on my own. I'm simply checking the view's window to get the current first responder. Then I return whether or not it is the active field editor for the view. Thus, I don't need a variable to track a value. – Ben Stock Feb 24 '15 at 06:08
  • Why you don't declare a get method instead of property? because property don't have any reason in that case. Just simply declare - (BOOL)bsd_isFieldEditorDelegate. – Huy Le Feb 24 '15 at 08:11
  • I would prefer the method declaration as well. However, for dot notation it is easier to use a property since it will autocomplete better. And for a name like isBsdFieldEditorDelegate autocompletion would be pretty nice! – Alex Feb 24 '15 at 14:13
  • @Alex: you can use dot syntax with method, too. Let's try declare `- (BOOL)isBsdFieldEditorDelegate` method, then use like this `object.isBsdFieldEditorDelegate`, you will understand what I mean. – Huy Le Feb 24 '15 at 16:09
  • Yeah I know you can, but it won't autocomplete it. So you will have to remember exactly how to type that entire thing every time you write it and then write it, pain in the butt – Alex Feb 24 '15 at 19:20
  • @Ares You CAN use a property in a category in Objective-C. What you can't do is add an instance variable. So if you do add a property, you must provide an implementation for the setter and getter that doesn't use an ivar. – rmaddy Aug 04 '15 at 15:55