Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining a property in iOS class extension

I would like to add a property to UITableView in a Class Extension:

@interface UITableViewController ()

@property NSString *entityString;

@end

Then I import the extension and then I use entityString property in a subclass of UITableViewController:

@implementation CustomerTableViewController

- (void)viewDidLoad {
    self.entityString = @"Customer";
    ...
    [super viewDidLoad];
}
...

Apple documentation says:

the compiler will automatically synthesize the relevant accessor methods (...) inside the primary class implementation.

But when I try to execute it I get this error:

-[CustomerTableViewController setEntityString:]: unrecognized selector sent to instance 0x737b670

What am I doing wrong? maybe the property cannot be accessed by subclasses?

like image 869
alazaro Avatar asked Sep 03 '12 18:09

alazaro


People also ask

Can we declare variable in extension Swift?

We can't add the stored properties to extensions directly but we can have the computed variables . Extensions in Swift can: Add computed instance properties and computed type properties.

What is the difference between Category & extension?

Category and extension both are basically made to handle large code base, but category is a way to extend class API in multiple source files while extension is a way to add required methods outside the main interface file.

How do you define a class in Objective-C?

In Objective-C, a class is itself an object with an opaque type called Class . Classes can't have properties defined using the declaration syntax shown earlier for instances, but they can receive messages.


1 Answers

A class extension is used to declare additional interface -- methods and properties -- whose implementation contract will be met within the class's primary @implementation.

Which is exactly why you can't add storage -- add ivars -- via a class extension. A class extension is an interface, no more, no less. @synthesize is what creates storage for @property declarations, but @synthesize of an @property can only appear in the @implementation of the class (whether explicitly or as a default behavior of the compiler).

Since you can't recompile the framework class, you can't add ivars to it.

@prashat's answer is one way to add storage to an existing class. However, going that route is generally undesirable; hanging state off of framework classes willy-nilly is a sign of poor design and will make your application significantly more difficult to maintain over time.

Far better to revisit your design, understand why you currently require attaching state to an object that can't directly contain it, and refactoring that requirement away.

like image 106
bbum Avatar answered Sep 28 '22 03:09

bbum