0

I am having trouble understanding how to overwrite a readonly method with a readwrite method written in a class extension. I have a readonly method in my header file, and in my implementation file I try to overwrite this by using a class extension. I have just asked about this in this thread where I include the code I am referring to: Having trouble with class extension in Objective C, mac OS. Getting error 'NSInvalidArgumentException', no visible @interface declares the selector

Here: Objective-C Succinctly: Categories and Extensions is another source I am using which is leading me to believe that I should be able to overwrite a readonly property declared in my header file with a readwrite property using a class extension. Please correct me if I'm wrong, I'm a complete novice just trying to figure out the basics.

Community
  • 1
  • 1
mepstein1218
  • 331
  • 5
  • 14

1 Answers1

0

The trick is you don't overwrite. You override. You write a read only method that adds any handling you need, then either calls super and returns that result or provides a custom result. (Referencing your own ivar or property or providing a calculated result)

Ok so after your edits.

Methods are slightly different. You're talking about properties. A property in Objective-C is basically declaring an ivar or instance variable that will (usually) have standard setter and getter methods. Those set or get the value of the ivar.

If you have a read only property in your header, or inherited from the superclass ( making it effectively in your header ) then yes, it is a common practice to redeclare the property as read write in the .m or implementation file. You add a class extension section. A class extension is basically an unnamed category on the class.

Example.

In .h

@interface Cat : NSObject <>
@property (readonly) NSInteger countOfCatTails;
@end

In .m
@interface Cat ()
@property NSInteger countOfCatTails;
@end
Community
  • 1
  • 1
uchuugaka
  • 12,679
  • 6
  • 37
  • 55
  • Sorry it's hard for me to understand what you're saying I'm not used to a lot of the coding jargon. What do you mean by "adds any handling"? Also when you say "returns that result", what result are you referring to? Lastly, what are you referring to with the parentheses? – mepstein1218 Sep 05 '14 at 14:28
  • That is literally exactly what I have, but I get the error, 'no visible @interface for "XYZPerson" declares the selector "setHeight" .' – mepstein1218 Sep 05 '14 at 17:45
  • I linked the code in my original question, "Having trouble with class extension in Objective C, mac OS. Getting error 'NSInvalidArgumentException', no visible @interface declares the selector" – mepstein1218 Sep 06 '14 at 02:45