2012-01-16 41 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

相关问题