2011-10-31 71 views
0
- (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)]; 
} 
} 

此行式IO /目標C:添加的NSString對象的NSMutableArray,NSMutableArray的是(空)

NSLog(@"%d: %@", urlIndex, [urlHistory objectAtIndex:urlIndex]); 

打印(無效),而作爲該行

NSLog(@"%d: %@", urlIndex, webpage.request.URL.absoluteString); 

打印該實際的網址。

在我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; 
} 

但我仍然不斷收到(空),當我進入我的數組。這是爲什麼?

+1

是你的問題中的拼寫錯誤,你正在添加對象到urlIndex(大概是一個整數)而不是urlHistory? – jrturton

+0

是的,修正了那個謝謝,但那是在一個不相關的線上... – Gibson

+1

有沒有不相關的線:) – jrturton

回答

6

聽起來像你的urlHistory對象是nil。在nil對象上調用-objectAtIndex:將返回nil。你可能忘了初始化你的-init中的urlHistory,或者你實際上並沒有調用你認爲你的-init方法(例如,如果你的VC是從一個筆尖加載它將使用-initWithCoder:而不是-initWithNibName:bundle:)。

爲了記錄在案,如果-objectAtIndex:NSArray不斷返回nil,這意味着數組本身是nil。既然NSArray不能存儲nil,-objectAtIndex:與有效索引將永遠不會返回nil-objectAtIndex:與無效索引將引發異常。因此,-objectAtIndex:返回nil的唯一方法是如果方法本身從未實際調用。

+0

在我的initWithNibName我已經有urlHistory = [[NSMutableArray alloc] init];我還可以做些什麼? – Gibson

+1

@Gibson:如果不能訪問所有的代碼,我無法真正回答這個問題,但是你可以通過簡單地註銷'NSLog(@「urlHistory:%@」,urlHistory)'來驗證這是怎麼回事。你應該看到'urlHistory:(null)'註銷。 –

+0

這確實是發生了什麼,但仍然不知道該怎麼辦... – Gibson