2011-09-24 46 views
0

时学坏访问错误,我这里有这个方法,轮流整数输入来自两个UITextFields成二进制代码:试图插入一个int到的NSMutableArray

//assume anything that isn't allocated here has been taken care of in the header file 
-(IBAction)valuesChanged 
{ 
    while ((![input1.text isEqualToString:@""]) && (![input2.text isEqualToString:@""])) 
    { 
     if (bitRange.selectedSegmentIndex == 0) {flowLimit = 8;} 
     else if (bitRange.selectedSegmentIndex == 1) {flowLimit = 16;} 
     else {flowLimit = 32;} 

     NSMutableArray* bin1 = [[NSMutableArray alloc] initWithCapacity:32]; 
     NSMutableArray* bin2 = [[NSMutableArray alloc] initWithCapacity:32]; 
     NSMutableArray* resBin = [[NSMutableArray alloc] initWithCapacity:32]; 

     input1Decimal = [input1.text intValue]; 
     input2Decimal = [input2.text intValue]; 

    int decimalDummy = input1Decimal; 
    while (decimalDummy > 0) 
    { 
     if (decimalDummy == 1) 
     { 
      [bin1 addObject:1]; 
      decimalDummy--; 
     } 
     else 
     { 
      [bin1 addObject:(decimalDummy % 2)]; //this is where I get the error 
      decimalDummy = decimalDummy/2; 
     } 
    } 

    decimalDummy = input2Decimal; 
    while (decimalDummy > 0) 
    { 
     if (decimalDummy == 1) 
     { 
      [bin2 addObject:1]; 
      decimalDummy--; 
     } 
     else 
     { 
      [bin2 addObject:(decimalDummy % 2)]; 
      decimalDummy = decimalDummy/2; 
     } 
    } 

    while ([bin1 count] < flowLimit) {[bin1 addObject:0];} 
    while ([bin2 count] < flowLimit) {[bin2 addObject:0];} 

    NSString* string1 = @""; 
    NSString* string2 = @""; 
    for (int i = 0; i < flowLimit; i++) 
    { 
     string1 = [[bin1 objectAtIndex:i] stringByAppendingString:string1]; 
     string2 = [[bin2 objectAtIndex:i] stringByAppendingString:string2]; 
    } 
    [output1 setText:string1]; 
    [output2 setText:string2]; 

    [bin1 release]; 
    [bin2 release]; 
    [resBin release]; 
} 
} 

我标记在那里我得到一个坏访问现场错误。任何人都知道为什么会发生?

回答

4

当然!您必须将对象放入NSArray s。 Plain int s不是对象,它们是原始类型。您可以在NSNumber S环绕他们,如果你想要把他们在NSArray

NSNumber *wrappedInt = [NSNumber numberWithInt:(decimalDummy % 2)]; 
[array addObject:wrappedInt]; 
+0

哦,对了。 Jeez,我讨厌这个!谢谢。 – RaysonK