I have a variable in a class, NSNumber. I want to pass the value of this var to another class var. The problem is that I release the object of the first class and obtain an error message when I try to set the value of the second class var.
In C++ this is so easy to do. But here with memory management and pointers confused me so much.
Solution code, for testing:
#import <Foundation/Foundation.h>
@interface A : NSObject
{
NSNumber *a;
}
@property (nonatomic, retain) NSNumber *a;
@end
int main(int argc, char *argv[])
{
NSAutoreleasePool *p = [[NSAutoreleasePool alloc] init];
A *instance1 = [[A alloc] init];
A *instance2 = [[A alloc] init];
[instance1 setA:[NSNumber numberWithFloat:5.43f]];
instance2.a = [instance1.a copy];
[instance1 release];
NSLog(@"Valor de la que sigue viva, parte2: %@", instance2.a);
[instance2 release];
[p release];
[pool drain];
return 0;
}
You should use a
retainproperty orcopythe instance variable:Read some docs about retain-counted memory management which is what Objective-C uses.