【问题标题】:saving NSString and then reading it back保存 NSString 然后读回来
【发布时间】:2010-07-05 00:11:07
【问题描述】:

大家好,这应该是一项简单的任务,但由于某种原因,我让它变得更难了……我正在尝试将一些文本从 XML 文件保存到 NSString。但是当我调试它时,字符串显示“超出范围”。

这是我的代码:

在我的 .h 文件中:

 @interface RootViewController : UIViewController<MBProgressHUDDelegate> {
     NSString *thePW;   
 }

还有我的 .m 文件:

 NSString *thePW;
 ...
 - (void)viewDidLoad {
 ...
    if(e == nil){
        NSString *response = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

        thePW = response; // <-- this is where it has "Out of scope"
        [response release];
    }
 }

 - (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
 {
     if (buttonIndex != [alertView cancelButtonIndex])
     {
        if (thePW == @"0000")
        {
           NSLog(@"correct!");
        }
     }
 }

【问题讨论】:

    标签: iphone cocoa-touch xcode iphone-sdk-3.0


    【解决方案1】:

    你也可以试试:

    thePW = [NSString stringWithFormat:@"%@",response];   // to assign the string value.
    

    和 比较字符串时:

    (thePW isEqualToString @"0000")
    

    【讨论】:

      【解决方案2】:

      在您的 .m 文件中删除对 thePW 的重新声明

      另外,如果您想在 PW 中保留 response 的值,请务必保留它。

      【讨论】:

      • 取出 .m 文件中的重新声明,但当我在 IF() 中检查它时,它仍然有“超出范围”...我也尝试使用以下方法保留它: @property (nonatomic,retain) NSString *thePW;但我仍然得到相同的“超出范围”。
      • 如果您将其设为属性,请将其分配为self.thePW = response;,以便调用 setter 方法。
      • drawnonward:这样做会给我错误“***由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'*** - [RootViewController thePW]:无法识别的选择器发送到实例0x3b04880 '"
      猜你喜欢
      • 1970-01-01
      • 2018-01-04
      • 2014-07-13
      • 2014-08-24
      • 2017-02-25
      • 1970-01-01
      • 2011-04-24
      • 1970-01-01
      • 2010-11-09
      相关资源
      最近更新 更多