保存NSString,然后将它读回
问题描述:
嘿所有,这应该是一个简单的任务,但由于某种原因,我正在努力...我试图从XML文件保存一些文本到NSString。但是,当我调试它,字符串说“超出范围”。保存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!");
}
}
}
答
你也可以尝试:
thePW = [NSString stringWithFormat:@"%@",response]; // to assign the string value.
和 而比较字符串:
(thePW isEqualToString @"0000")
答
下降thePW在您.m文件
的重新声明另外,如果你想保留响应的值,那么一定要保留它。
拿出重新声明在.m文件,但它仍然有它当“超出范围”我检查它在IF()... 我也试图保留它使用这个:@property(nonatomic,retain)NSString * thePW;但我仍然得到“超出范围”。 – StealthRT 2010-07-05 00:36:46
如果你使它成为一个属性,用'self.thePW = response;'来分配它,这样调用setter方法。 – drawnonward 2010-07-05 01:14:58
drawonward:这样做会给我错误的“***终止应用程序由于未捕获的异常'NSInvalidArgumentException',原因:'*** - [RootViewController thePW]:无法识别的选择器发送到实例0x3b04880'” – StealthRT 2010-07-05 02:11:02