0

I have this method here that turns integer inputs from two UITextFields into binary code:

//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];
}
}

I labeled the spot where I'm getting a bad access error. Anyone know why it's happening?

1 Answer 1

4

Sure! You have to put objects in NSArrays. Plain ints aren't objects, they're primitive types. You can wrap them in NSNumbers if you want to put them in an NSArray:

NSNumber *wrappedInt = [NSNumber numberWithInt:(decimalDummy % 2)];
[array addObject:wrappedInt];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.