Objective c property issue
-
29-04-2021 - |
Pregunta
In my Settings.h
file I have the line
@property (nonatomic, retain) NSArray * connections;
Also in the Settings.m
file there are importing:
#import "Settings.h"
and later I provide the implementation
- (NSArray*)connections
{
return connections;
}
- (void)setConnections:(NSArray*)_connections
{
connections = _connections;
// do some more stuff
}
But both in getter and setter I get an error about use of undeclared identifier 'connections'
I have no idea what do I do wrong, so any of your help would be greatly appreciated!
Solución
You are, quite correctly, trying to use an ivar (called connections
) as a backing store for your property, also called connections
;
To get it to work, you should simply declare an ivar like this:
// Settings.h
NSArray * connections;
It should go between the curly brackets of the class declaration, like this:
@interface MyClass : MySuperClass {
NSArray *connections;
// More ivars...
}
@property (nonatomic, assign) NSArray *connections;
@end
Otros consejos
in Settings.m
@synthesize connections = _connections;
in setting.h
In Settings.h
add an instance variable NSArray *connections;
(if it's not there already), then in Settings.m
just below @implementation
add this:
@dynamic connections;
Another option is to remove you accessors and go for @synthesize connections;
instead of dynamic
and the accessor methods will be created for you.