Fried Rice
Fried Rice

Reputation: 3713

Accessing NSString between methods causes app crash

I apologise if this is a bad question as I'm new to iOS development. So here is my problem: I declared a class variable NSString, the string is assigned a string value from a textView but when i try to access the string from other method, the app crashes. Here's the code:

Interface: (ClassName.h)

@interface ClassName: UIViewController <UITextViewDelegate>{}
@property (nonatomic, assign)NSString *strSomeText;
@end

Implementation: (ClassName.m)

@implementation
@synthesize strSomeText;
- (void)textViewDidChange:(UITextView *)textView{
    strSomeText = textView.text;
    NSLog(@"%@", strSomeText); //This line works just fine
}
- (void)textViewDidEndEditing:(UITextView *)textView{
    NSLog(@"%@", strSomeText); //this line causes the app to crash
}
@end

Thanks for the help! Loc.

Upvotes: 1

Views: 235

Answers (1)

Jesse Rusak
Jesse Rusak

Reputation: 57168

Your problem is likely due to the fact that you're using assign for your property. This will mean that the string can be deallocated while you still have a reference to it. Try using copy instead:

@property (nonatomic, copy) NSString *strSomeText;

Then you should use your property accessor in your textViewDidChange: method:

self.strSomeText = textView.text;

Upvotes: 4

Related Questions