0

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!

Solomiya
  • 310
  • 4
  • 15

3 Answers3

2
in Settings.m 

@synthesize connections = _connections;

in setting.h
talha2k
  • 24,937
  • 4
  • 62
  • 81
yuquan0821
  • 21
  • 4
0

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
Monolo
  • 18,205
  • 17
  • 69
  • 103
-1

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.

Simon
  • 3,667
  • 1
  • 35
  • 49
  • yes, it would be reasonable to remove my own accessors, but I have to specify few more actions in setter, which I didn't mention in here. – Solomiya Feb 13 '12 at 10:49
  • Hmm, when I add `@dynamic connections;` the error doesn't disappear. – Solomiya Feb 13 '12 at 10:53
  • Oh, no-no, I made a mistake :) Now, everything works great! Thank you very much! – Solomiya Feb 13 '12 at 10:55
  • What is the need for `@dynamic connections`? – Monolo Feb 13 '12 at 11:02
  • Hmm, I don't know - I just tried to remove it and nothing changed, everything works like before – Solomiya Feb 13 '12 at 11:53
  • As I understand it `@dynamic` is needed in order to access `connections` as a property, e.g. in bindings or using `mySetting.connection` notation. From what I've read you use it when you want to write the property accessors yourself. As such, `[mySetting connections]` would work without the `@dynamic`, while `mySetting.connections` would not. I might be wrong on this though. – Simon Feb 13 '12 at 13:10
  • I thought that `@dynamic` is required if I'm going to define accessors later, e.g. in subclasses. What about notation, in my application I write `currentSettings.connections = [fields objectForKey:@"connections"];` and it works fine – Solomiya Feb 13 '12 at 13:22
  • Yes, I am obviously wrong here. http://stackoverflow.com/a/4621968/222245 offers a pretty good description of `@dynamic`. – Simon Feb 13 '12 at 13:43
  • You know, probably you're not - http://stackoverflow.com/questions/4524954/what-is-common-case-for-dynamic-usage/4524983#4524983 :) As I got, it's just a question of your programming style – Solomiya Feb 13 '12 at 15:05