2009-08-25 188 views
1

我有一個變量值奇怪的問題。這是該代碼(這是一個類方法的一部分):iphone nslog損壞的數據

MyAppDelegate *pDelegate = [[UIApplication sharedApplication] delegate]; 
SomeDictionaryData *appData = [pDelegate.theData retain]; 
NSLog(@"my instance var: %@",cardIndex); // outputs "my instance var: 4" 
NSDictionary *currentCard = [[NSDictionary alloc] initWithDictionary:[appData.cards objectAtIndex:cardIndex]];; 
// the above line breaks the app 
[currentCard release]; 
[appData release]; 

我使用與objc_exception_throw斷點調試器。 objectAtIndex在那裏收到的輸入顯示爲值= 13760640. appData的cards屬性是NSArray,它顯然沒有1000萬以上的項目,所以我得到了一個超出界限的錯誤。我試着用(int)cardIndex鑄造沒有更好的結果。奇怪的是一些其他類中的類似代碼很好。

這是我想要在整個應用程序中使用的一些數據,所以我有一個Model類,它在AppDelegate中被初始化爲theData,然後被不同的ViewController加以使用。在其他一些ViewController上成功訪問後,會出現此錯誤(該錯誤也會保留/釋放)。

任何幫助將不勝感激。

回答

0

使用[cardIndex unsignedIntValue]objectAtIndex:行。

您不能給objectAtIndex:一個指針,因爲它需要一個無符號整數。

例如:

NSDictionary *currentCard = [[NSDictionary alloc] initWithDictionary:[appData.cards objectAtIndex:[cardIndex unsignedIntValue]]]; 

編輯:

這聽起來像cardIndexint但某處沿着線它被設置爲NSNumber實例。作爲黑客,請使用[(id)cardIndex unsignedIntValue]。如果這有效,那麼這意味着你使用了cardIndex的錯誤類型(它應該是NSNumber,而不是int)。

+0

謝謝兄弟!雖然我得到一個「無效的接收器類型int」警告。 – mga 2009-08-25 02:44:27

+0

嘗試投射爲(NSUInteger)並仍然收到警告。其他一切正常,但...我只是不喜歡警告。 – mga 2009-08-25 02:45:30

+0

對!將所有必要的引用轉換爲NSNumber。謝謝! – mga 2009-08-25 03:45:14