Reputation: 51
In my .h
file
@interface DemoApp : UIViewController {
UITextView *textView;
@private
NSString *status;
NSInteger *track1Len,*track2Len;
}
@property (nonatomic,retain) NSString *status;
In .m
file
@synthesize status;
- (void)onDecodeCompleted:(NSString *)sts
{
status=sts;
NSLog(@"status is %@",status);
}
- (IBAction)processButton:(id)sender
{
NSLog(@"status is %@",status);
// here I am getting null value
}
I just to access my variable throughout my class file.In ondecodecompleted method,I am getting the value that is passed as parameter but after the process button is pressed I am getting null value.
Upvotes: 0
Views: 58
Reputation: 8664
The Reason why you are losing your iVar is because you are not getting ownership
on your variable when you are setting it directly.
You need to do status = [sts copy];
OR status = [sts retain]
to take ownership.
If you don't take ownership status
will have the life span of sts
which is probably not what you want.
Accessing iVar directly is usually a bad idea because of the local memory management that you need to remember to do right each time.
The best practice is to use your @property
with dot syntax
. the property will take care of the memory management of setters accordingly to the one you will have declared.
Upvotes: 0
Reputation: 46813
try: self.status
in both places.
- (void)onDecodeCompleted:(NSString *)sts
{
self.status=sts;
NSLog(@"status is %@",self.status);
}
- (IBAction)processButton:(id)sender
{
NSLog(@"status is %@",self.status);
// here I am getting null value
}
Also since you are using NSString
which has a mutable subclass, you should mark it as copy
and not retain
@property (nonatomic,copy) NSString *status;
Upvotes: 2