2011-09-24 76 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