函数应该返回浮点数,但它搞砸了!

And*_*dre 3 iphone xcode objective-c

我在这里疯了.

我有一个应该返回一个浮点数的函数:

- (float) getHue:(UIColor *)original
{
    NSLog(@"getHue");
    const CGFloat *componentColors = CGColorGetComponents(original.CGColor);

    float red = componentColors[0];
    float green = componentColors[1];
    float blue = componentColors[2];

    float h = 0.0f;
    float maxChannel = fmax(red, fmax(green, blue));
    float minChannel = fmin(red, fmin(green, blue));
    if (maxChannel == minChannel)
        h = 0.0f;
    else if (maxChannel == red)
        h = 0.166667f * (green - blue) / (maxChannel - minChannel) + 0.000000f;
    else if (maxChannel == green)
        h = 0.166667f * (blue - red) / (maxChannel - minChannel) + 0.333333f;
    else if (maxChannel == blue)
        h = 0.166667f * (red - green) / (maxChannel - minChannel) + 0.666667f;
    else
        h = 0.0f;

    if (h < 0.0f)
        h += 1.0f;

    NSLog(@"getHue results: %f", h);

    return h;
}
Run Code Online (Sandbox Code Playgroud)

NSLog将正确跟踪它(即:0.005),但该函数的实际返回值为NULL.

我试过在很多方面获得这个价值但它永远不会奏效.

float originalHue = [self getHue:original];
Run Code Online (Sandbox Code Playgroud)

导致构建错误,因为它说:"初始化中不兼容的类型"

float *originalHue = [self getHue:original];
Run Code Online (Sandbox Code Playgroud)

导致返回null.

我尝试过其他方法,但它实际上从未真正获得价值.

有什么想法吗?

干杯,安德烈

Alf*_*nso 5

你有没有在你的班级界面中宣布你的方法?如果是这样,您是否每次发生事故都表明有不同的返回值(例如id)?

如果未在接口中声明它,编译器会将返回值视为id,因此您需要在接口中声明它或将返回值强制转换为a float.