【问题标题】:Accessing NSString between methods causes app crash在方法之间访问 NSString 会导致应用崩溃
【发布时间】:2012-08-06 14:51:00
【问题描述】:

如果这是一个不好的问题,我深表歉意,因为我是 iOS 开发的新手。所以这是我的问题:我声明了一个类变量 NSString,该字符串从 textView 分配了一个字符串值,但是当我尝试从其他方法访问该字符串时,应用程序崩溃了。代码如下:

接口:(ClassName.h)

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

实现:(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

感谢您的帮助! 地点。

【问题讨论】:

    标签: iphone string methods nsstring


    【解决方案1】:

    您的问题很可能是因为您将assign 用于您的财产。这意味着可以在您仍然拥有对它的引用时释放该字符串。尝试改用copy

    @property (nonatomic, copy) NSString *strSomeText;
    

    那么你应该在 textViewDidChange: 方法中使用你的属性访问器:

    self.strSomeText = textView.text;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多