- (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)];}}这条线 NSLog(@"%d: %@", urlIndex, [urlHistory objectAtIndex:urlIndex]);打印(空),而与此行 NSLog(@"%d: %@", urlIndex, webpage.request.URL.absoluteString);打印实际的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;}但是当我访问数组时,我仍然保持为(空)。这是为什么? 最佳答案 听起来您的urlHistory对象是nil。在-objectAtIndex:对象上调用nil将返回nil。您可能忘记了在urlHistory中初始化-init,或者您实际上没有在调用自己认为的-init方法(例如,如果从笔尖加载VC,它将使用-initWithCoder:而不是)。作为记录,如果-initWithNibName:bundle:上的-objectAtIndex:返回NSArray,则表示数组本身为nil。由于nil无法存储NSArray,具有有效索引的nil将永远不会返回-objectAtIndex:,而具有无效索引的nil将引发异常。因此,-objectAtIndex:返回nil的唯一方法是从未真正调用过方法本身。 10-08 11:51