0

This works fine:

NSString *myVariable;

- (IBAction) doFirstAction {
myVariable = @"123456789";
}

- (IBAction) doSecondAction {
NSLog(@"%@",myVariable);
}

However, if I do this (substituting the @"123456789" for some code which returns the same value ie "123456789") I cannot access the value in doSecondAction.

NSString *myVariable;

- (IBAction) doFirstAction {
myVariable = [imageNode getAttributeName:@"value"];
}

- (IBAction) doSecondAction {
NSLog(@"%@",myVariable);
}

Any clue as to why I cant access myVariable outside of doFirstAction?

StuR
  • 12,042
  • 9
  • 45
  • 66

2 Answers2

2

you need to retain it

myVariable = [[imageNode getAttributeName:@"value] retain];

see also

Community
  • 1
  • 1
AndersK
  • 35,813
  • 6
  • 60
  • 86
0

Another way of doing this would be to define an accessor method that does the retaining for you. Thus in your interface definition:

@property (retain) NSString *myVariable;

and later in your implementation:

@synthesize myVariable;

Now Xcode generates getter and setter methods for you that will handle the retain statement for you. However, you now need to use the dot notation, since the equals sign is not overridden, and your setter method is not called unless you do so:

self.myVariable = [imageNode getAttributeName:@"value"];
Joost Schuur
  • 4,417
  • 2
  • 24
  • 39