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.
Your problem is likely due to the fact that you’re using
assignfor your property. This will mean that the string can be deallocated while you still have a reference to it. Try usingcopyinstead:Then you should use your property accessor in your
textViewDidChange:method: