Gib*_*son 0 objective-c nsstring nsmutablearray ios
- (void)webViewDidFinishLoad:(UIWebView *)webView {
if (urlIndex == maxIndex) {
maxIndex = maxIndex + 1;
NSString* sURL = webpage.request.URL.absoluteString;
[urlHistory addObject:sURL];
NSLog(@"%d: %@", urlIndex, [urlHistory objectAtIndex:urlIndex]);
NSLog(@"%d: %@", urlIndex, webpage.request.URL.absoluteString);
[sURL release];
urlIndex = urlIndex + 1;
}
else {
[urlHistory insertObject:webView.request.URL.absoluteString atIndex:(urlIndex - 1)];
}
}
Run Code Online (Sandbox Code Playgroud)
这条线
NSLog(@"%d: %@", urlIndex, [urlHistory objectAtIndex:urlIndex]);
Run Code Online (Sandbox Code Playgroud)
print(null),同时作为这一行
NSLog(@"%d: %@", urlIndex, webpage.request.URL.absoluteString);
Run Code Online (Sandbox Code Playgroud)
打印实际的URL.
在我的initWithNibName上,我有:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:@"Tab1ViewController" bundle:nil];
if (self) {
urlHistory = [[NSMutableArray alloc] init];
urlIndex = 0;
maxIndex = 0;
}
return self;
}
Run Code Online (Sandbox Code Playgroud)
但是当我访问我的数组时,我仍然继续得到(null).这是为什么?
这听起来像你的urlHistory对象nil.调用-objectAtIndex:一个关于nil对象将返回nil.您可能忘记urlHistory在您的初始化-init,或者您实际上并没有调用-init您认为自己的方法(例如,如果您的VC是从它将使用的nib加载-initWithCoder:而不是-initWithNibName:bundle:).
为了记录在案,如果-objectAtIndex:在一个NSArray曾经返回nil,这意味着阵列本身nil.由于NSArray无法存储nil,-objectAtIndex:具有有效索引永远不会返回nil,并且-objectAtIndex:带有无效索引会抛出异常.所以-objectAtIndex:返回nil 的唯一方法是如果方法本身从未被实际调用过.
| 归档时间: |
|
| 查看次数: |
1405 次 |
| 最近记录: |