2012-01-16 74 views
0

我想將數字存儲在NSMutableArray中,但是當我檢查它時,我得到的只是垃圾。存儲在NSMutableArray中的NSNumbers

-(void)storeIntoArray:(int)indexInt 
{ 
    NSNumber *indexNum = [[NSNumber alloc] initWithInt:indexInt]; 
    [last100 insertObject:indexNum atIndex:prevCount]; 
    prevCount++; 

    if(prevCount > 100) 
    { 
     prevCount = 0; 
     cycledOnce = YES; 
    } 

} 

-(BOOL)checkArray:(int)indexInt 
{ 
for(int i = 0;i < [last100 count];i++) 
    { 
     NSLog(@"Stored: %d",[last100 objectAtIndex:i]); 
     if (indexInt == (int)[last100 objectAtIndex:i]) { 
      NSLog(@"Same entry, trying again"); 
      return YES; 
     } 
    } 
    return NO; 
} 

當我檢查,我會回來的值一樣Stored: 110577680,只是不同的垃圾值。

+0

不是這是你的問題,而是你泄漏你存儲的每一個數字。 – 2012-01-16 04:26:12

+0

好的。發佈indexNum? – Chris 2012-01-16 04:27:17

+0

請注意,使用NSMutableSet來檢測重複條目(如果這是您的意圖)可能會更有效。 – 2012-01-16 04:36:58

回答

1

您正在打印NSNumber對象地址,而不是其值。嘗試使用intValue。或者,如果要直接打印對象,請使用%@而不是%d

0
NSLog(@"Stored: %d",[last100 objectAtIndex:i]); 

應該是:
的NSLog(@ 「存儲:%@」,[last100 objectAtIndex:ⅰ]);

NSNumber是一個對象不是整數。

+0

更重要的是,它應該是'if(indexInt == [[last100 objectAtIndex:i] intValue])''。 – 2012-01-16 04:34:29