doubleValue上的选择器无效



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的检索过程中,而不是填充过程中。这意味着这本字典的数量不是问题,但这可能与它在检索数据时遇到问题有关。

那么,是什么原因导致了这个错误呢?

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

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

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 ...

而CCD_ 2是CCD_。井getEffectivenessString期望NSNumber,而不是NSMutableArray。所以它抱怨。此外,我认为你打算让这个方法采用字符串,而不是数字,尽管我不明白为什么你不在加载它们时(而不是在使用它们时)进行强制转换。

由于Objective C不支持强类型数组或字典,因此在未来防范这种情况的最佳方法是更合乎逻辑地命名变量。当您尝试调用一个方法时,它应该很突出,该方法需要一个带数组的数字。

最新更新