我这里有这个方法,它将两个UITextField的整数输入转换为二进制代码:
//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];
}
}
我标记了我遇到错误访问错误的位置。有人知道为什么会这样吗?
答案 0 :(得分:4)
当然!你必须在NSArray
s中放置对象。普通int
不是对象,它们是原始类型。如果您想将它们放在NSNumber
:
NSArray
中
NSNumber *wrappedInt = [NSNumber numberWithInt:(decimalDummy % 2)];
[array addObject:wrappedInt];