- (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)];
}
}
This line
NSLog(@"%d: %@", urlIndex, [urlHistory objectAtIndex:urlIndex]);
prints (null), while as this line
NSLog(@"%d: %@", urlIndex, webpage.request.URL.absoluteString);
prints the actual URL.
On my initWithNibName I have:
- (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;
}
But I still keep getting (null) when I access my array. Why is that?
It sounds like your
urlHistoryobject isnil. Calling-objectAtIndex:on anilobject will returnnil. You probably forgot to initializeurlHistoryin your-init, or you’re not actually calling the-initmethod you think you are (e.g. if your VC is loaded from a nib it will be using-initWithCoder:instead of-initWithNibName:bundle:).For the record, if
-objectAtIndex:on anNSArrayever returnsnil, it means the array itself isnil. SinceNSArraycannot storenil,-objectAtIndex:with a valid index will never returnnil, and-objectAtIndex:with an invalid index will throw an exception. So the only way for-objectAtIndex:to return nil is if the method itself is never actually called.