使用同一类方法的类方法时出现Xcode错误

时间:2012-04-24 16:03:13

标签: xcode pointers casting xcode4.2 class-method

我有一个具有类方法“getSimulatedPricesFrom”的类。它将在执行期间从同一个类调用方法“projectFromPrice”。但是在行sTPlus1行中,我遇到了2个错误:

1) Class method "projectFromPrice" not found

2) Pointer cannot be cast to type "double" 

有没有人知道为什么?我已经在.h文件中声明了该方法 以下是AmericanOption.m文件中编码的一部分:

#import "AmericanOption.h"

@implementation AmericanOption

+(NSMutableArray*)getSimulatedPricesFrom:(double)s0 withRate:(double)r0 withVol:(double)v0 withDays:(int)D withPaths:(int)N
{
    double daysPerYr = 365.0;
    double sT;
    double sTPlus1;
    sT = s0;
...
    sTPlus1 = (double)[AmericanOption projectFromPrice:sT, r0/daysPerYr, v0/daysPerYr, 1/daysPerYr];
...
    return arrPricePaths;
}

+(double)projectFromPrice:(double)s0 withRate:(double)r0 withVol:(double)v0 withDt:(double)dt
{
    ...
}

2 个答案:

答案 0 :(得分:1)

看起来您应该按如下方式调用projectFromPrice方法:

sTPlus1 = [AmericanOption projectFromPrice:sT 
                                  withRate:r0/daysPerYr 
                                   withVol:v0/daysPerYr 
                                    withDt:1/daysPerYr];

在您的示例代码中,您只是提供逗号分隔的参数列表。您应该使用方法的命名参数。

两个错误中的第一个是因为方法projectFromPrice:与方法projectFromPrice:withRate:withVol:withDt:不同。

projectFromPrice:withRate:withVol:withDt:是实际存在的方法,可能在您的界面(.h文件)中定义。 projectFromPrice:是您尝试调用的方法,但它不存在。

第二个错误是编译器假设未定义的projectFromPrice:方法返回id(一个指针)而无法转换为double的结果。

答案 1 :(得分:0)

这是您调用第二种方法的方式,这似乎是问题所在。试试这个,而不是:

+(NSMutableArray*)getSimulatedPricesFrom:(double)s0 withRate:(double)r0 withVol:(double)v0 withDays:(int)D withPaths:(int)N
{
    double daysPerYr = 365.0;
    double sT;
    double sTPlus1;
    sT = s0;
...
    sTPlus1 = (double)[AmericanOption projectFromPrice:sT withRate:r0/daysPerYr withVol:v0/daysPerYr withDt:1/daysPerYr];
...
    return arrPricePaths;
}

+(double)projectFromPrice:(double)s0 withRate:(double)r0 withVol:(double)v0 withDt:(double)dt
{
    ...
}