doubleValue上的选择器无效

时间:2013-08-04 17:54:56

标签: objective-c

Objective-C有点新鲜,所以请耐心等待。

首先,我使用FMDB库进行SQLite管理。

我使用以下方法填充NSMutableDictionary:

//....
while([effectivenessResults next]) //this loops through the results of a query (verified that this works)
    {
        NSMutableArray *dFactors = [[NSMutableArray alloc]init];
        if([resultDict objectForKey:[effectivenessResults stringForColumn:@"tName"]])
        {
            dFactors = [resultDict objectForKey:[effectivenessResults stringForColumn:@"tName"]];
        }
        NSNumber *effectivenessValToAdd = [NSNumber numberWithDouble:[effectivenessResults doubleForColumn:@"dFactor"]/100];
        [dFactors addObject:[NSMutableString stringWithFormat:@"%@",effectivenessValToAdd]];
        [resultDict setObject:dFactors forKey:[effectivenessResults stringForColumn:@"tName"]];
    }

我正确地返回了数组(我已经验证了这一点)。然后,我使用以下方法在其他地方访问此NSMutableDictionary:

for(id type in tEffect) //tEffect is the NSMutableDictionary, returned from the previous code (there known as resultDict)
{
    effectivenessString = [self getEffectivenessString:[tEffect objectForKey:type]];

    tInfo = [NSMutableString stringWithFormat:@"%@", [tInfo stringByAppendingFormat:@"%@: %@\n", type, effectivenessString]];

}

调用以下两种方法:

-(NSMutableString *)getEffectivenessString:(NSNumber *) numberPassedIn
{
    double dFactor = [numberPassedIn doubleValue];
    //adds the above value to a string, this will not affect anything
}

-(NSNumber *) listProduct: (NSMutableArray *)listOfValues //calculates the product of an NSMutableArray of numbers
{
NSNumber *product=[NSNumber numberWithDouble:1.0];

for(int i = 0; i < [listOfValues count]; i++)
{
    NSNumber *newVal = [listOfValues objectAtIndex:i];
    product = [NSNumber numberWithDouble:[product doubleValue] * [newVal doubleValue]];
}
return product;
}

因此,当我调用这些方法时,我收到以下错误:

2013-08-04 13:52:04.514 effectCalculator[45573:c07] -[__NSArrayM doubleValue]:              
unrecognized selector sent to instance 0x8c19e00
2013-08-04 13:52:04.521 effectCalculator[45573:c07] *** Terminating app due to uncaught     
exception 'NSInvalidArgumentException', reason: '-[__NSArrayM doubleValue]: unrecognized 
selector sent to instance 0x8c19e00'

需要注意的重要事项:检索时会出现此错误,而不是填充NSMutableDictionary。这意味着这本字典的填充不是一个问题,但它可能与它在检索数据时遇到问题的原因有关。

那么可能导致此错误的原因是什么?

1 个答案:

答案 0 :(得分:2)

您的代码非常难以理解。将来请发布一个最小的样本,它可以编译,或者至少是一个可理解的代码块。

话虽如此,我相信你的问题在于:

for(id type in tEffect) //tEffect is the NSMutableDictionary, returned from the previous code (there known as resultDict)
{
    effectivenessString = [self getEffectivenessString:[tEffect objectForKey:type]];

resultDict包含哪些内容?

[resultDict setObject:dFactors ...

dFactorsNSMutableArray。那么getEffectivenessString需要NSNumber,而不是NSMutableArray。所以它抱怨。另外我认为你打算采用字符串而不是数字的方法,虽然我不明白为什么你不加载它们时(而不是你使用它们)。

由于Objective C不支持强类型数组或字典,因此将来防御这种情况的最佳方法是更加逻辑地命名变量。当你试图调用一个期望带数组的数字的方法时它应该突出。