为什么NSMutablearray会一直返回null?

时间:2014-06-28 10:38:52

标签: objective-c methods nsmutablearray ddmathparser

我正在生成一个随机方程式,如2 * 3 + 4 .....并使用DDMathparser来评估它。这里我有一个类方法,它应该返回一个随机方程(存储在一个可变数组中),只有它的计算结果为整数。 然而它一直在返回Null,我无法弄清楚为什么。请帮帮我。

#import "Equation.h"
#import "DDMathParser.h"

@implementation Equation
-(NSMutableArray*)randEquation{
    NSMutableArray* usableEquation=[[NSMutableArray alloc]init];
    while(1){
    NSArray *nums = @[@"1", @"2", @"3", @"4", @"5",@"6",@"7",@"8",@"9"];
    unsigned index1=arc4random()%9;
    NSString* num = [NSString stringWithFormat:@"%@", [nums objectAtIndex:index1]];
    NSArray *symbols = @[@"+", @"-", @"*", @"/"];
    unsigned index=arc4random()%4;
    NSString* symb = [NSString stringWithFormat:@"%@", [symbols objectAtIndex:index]];
        NSMutableArray *arrayOfSymbolsAndNumbers = [[NSMutableArray alloc] init];
        for( int i=0;i<=10;i++){
            if (i%2==0) {
                [arrayOfSymbolsAndNumbers addObject:num];
            }
            else{
                [arrayOfSymbolsAndNumbers addObject:symb];
            }

        }
        NSMutableString *stringOfSymbolsAndNumbers=[[NSMutableString alloc]init];
        for (NSObject * obj in arrayOfSymbolsAndNumbers)
        {
            [stringOfSymbolsAndNumbers appendString:[obj description]];


        }
        usableEquation=arrayOfSymbolsAndNumbers;
        NSNumber *result=[stringOfSymbolsAndNumbers numberByEvaluatingString];
        float resultFloat = [result floatValue];
        float checker=resultFloat;
        if (floor(checker)==checker) {
            break;
        }
        else{
            continue;
        }
    }
    return usableEquation;
}

@end

1 个答案:

答案 0 :(得分:0)

  

NSLog(@&#34;数组的内容是%@&#34;,[方程式randEquation]);

根据您的代码,此日志输出The content of array is(null)表示equation为零。您的randEquation(虽然效率不高)看起来没问题,但问题是您在运行日志语句时尚未创建equation实例。