1

我是一个使用 RPN 计算器的新手,我正在尝试设置一个由两个成员数组组成的数组来存储字符串/值对,以便以后可以使用它们来显示按下的数字/运算符的完整序列到达用户现在所在的任何地方。问题是,无论我做错了什么都意味着数组不会填充。关于如何解决这个问题的任何想法?

这是我的代码(oneStringValuePair并且arrayOfStringValuePairs是在 .h 中声明(nonatomic, strong)并在 .m 中合成的 NSMutableArrays):

NSLog(@"result is float: %f",result);

self.formattedNumber = [NSString stringWithFormat:@"( %@%@%@ )",firstNumber,operation,secondNumber];

NSLog(@"String is %@",self.formattedNumber);

NSMutableArray *pair = self.oneStringValuePair;
NSMutableArray *array = self.arrayOfStringValuePairs;

pair = [pair init];
array = [array init];

if(pair==nil)
{
    NSLog(@"pair is nil.");
}

if (array==nil)
{
    NSLog(@"array is nil.");
}

[pair addObject:self.formattedNumber];

NSLog(@"first array element description: %@", [[pair lastObject] description]);
[pair addObject:[NSNumber numberWithFloat:result]];

NSLog(@"second array element description: %@",[[pair lastObject] description]);
NSLog(@"pair count: %@",self.oneStringValuePair.count);

if (self.arrayOfStringValuePairs)
{
    [array addObject:pair];
}

NSLog(@"count of pairs:  %@",array.count);

这是 NSLog 的5 3 + 6 /

2012-08-04 08:02:54.550 Calculator[83891:f803] result is float: 8.000000
2012-08-04 08:02:54.552 Calculator[83891:f803] String is ( 3+5 )
2012-08-04 08:02:54.552 Calculator[83891:f803] pair is nil.
2012-08-04 08:02:54.553 Calculator[83891:f803] array is nil.
2012-08-04 08:02:54.554 Calculator[83891:f803] first array element description: (null)
2012-08-04 08:02:54.555 Calculator[83891:f803] second array element description: (null)
2012-08-04 08:02:54.555 Calculator[83891:f803] pair count: (null)
2012-08-04 08:02:54.556 Calculator[83891:f803] count of pairs:  (null)
2012-08-04 08:02:55.987 Calculator[83891:f803] result is float: 0.750000
2012-08-04 08:02:55.988 Calculator[83891:f803] String is ( 6/8 )
2012-08-04 08:02:55.989 Calculator[83891:f803] pair is nil.
2012-08-04 08:02:55.990 Calculator[83891:f803] array is nil.
2012-08-04 08:02:55.991 Calculator[83891:f803] first array element description: (null)
2012-08-04 08:02:55.992 Calculator[83891:f803] second array element description: (null)
2012-08-04 08:02:55.993 Calculator[83891:f803] pair count: (null)
2012-08-04 08:02:55.994 Calculator[83891:f803] count of pairs:  (null)
4

3 回答 3

1

你有像下面这样初始化 NSMutableArray 吗?

NSMutableArray *pair = [NSMutableArray array];
于 2012-08-04T12:18:11.987 回答
1

您的代码中几乎没有问题

NSLog(@"result is float: %f",result);

self.formattedNumber = [NSString stringWithFormat:@"( %@%@%@ )",firstNumber,operation,secondNumber];

NSLog(@"String is %@",self.formattedNumber);

// ***** add 
if (self.oneStringValuePair == nil)
{
    self.oneStringValuePair = [NSMutableArray array];
}

if (self.arrayOfStringValuePairs == nil)
{
    self.arrayOfStringValuePairs = [NSMutableArray array];
}

// *****
NSMutableArray *pair = self.oneStringValuePair;
NSMutableArray *array = self.arrayOfStringValuePairs;

//pair = [pair init];   // `init` is used to initialize object
//array = [array init]; // but not allocate object, if array is nil, [array init] will return nil

if (pair==nil)
{
    NSLog(@"pair is nil.");
}

if (array==nil)
{
    NSLog(@"array is nil.");
}

[pair addObject:self.formattedNumber];
NSLog(@"first array element description: %@", [[pair lastObject] description]);

[pair addObject:[NSNumber numberWithFloat:result]];
NSLog(@"second array element description: %@",[[pair lastObject] description]);

// the line below will crash if self.oneStringValuePair is not nil
//NSLog(@"pair count: %@",self.oneStringValuePair.count);
NSLog(@"pair count: %d",self.oneStringValuePair.count);

if (self.arrayOfStringValuePairs)
{
    [array addObject:pair];
}

// the line below will crash if array is not nil
//NSLog(@"count of pairs:  %@",array.count);
NSLog(@"count of pairs:  %d",array);
于 2012-08-04T12:27:06.670 回答
1

这是做什么的?

NSMutableArray *pair = self.oneStringValuePair;
NSMutableArray *array = self.arrayOfStringValuePairs;
pair = [pair init];
array = [array init];

您可以打印并检查self.oneStringValuePair&是否self.arrayOfStringValuePairs为 nil。如果为 nil,则像这样分配这两个对象。

self.oneStringValuePair = [[[NSMutableArray alloc] init] autorelease]

也不需要打电话

pair = [pair init];
array = [array init];

尝试删除它。每当您为其分配对象时,都无需执行 init (Initializer)。

于 2012-08-04T12:43:55.260 回答