函数应该返回浮点数,但是它会被打乱!

时间:2022-12-24 17:07:13

I'm going crazy here.

我要疯了。

I have a function that should return a float number:

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

- (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;
}

The NSLog will trace it correctly (i.e: 0.005), but the actual return value of the function is NULL.

NSLog将正确地跟踪它(i)。e: 0.005),但函数的实际返回值为NULL。

I've tried getting that value in so many ways and it never works.

我尝试了很多方法来获得这个价值,但从来没有成功过。

float originalHue = [self getHue:original];

results in a building error, as it says: "incompatible types in initialization"

导致一个构建错误,如它所说:“初始化中的不兼容类型”

float *originalHue = [self getHue:original];

results in a null return.

结果为空返回。

I've tried other ways, but it never actually gets the value properly.

我尝试过其他的方法,但是它从来没有正确地获得值。

Any thoughts?

任何想法吗?

Cheers guys, Andre

干杯,安德烈

4 个解决方案

#1


5  

Have you declared your method in the interface of your class? If so, did you per accident indicate a different return value (e.g. id)?

在类的接口中声明了方法吗?如果是,你是否每次意外都表示不同的返回值(例如id)?

If it is not declared in your interface the compiler will treat the return value as id, so you would either need to declare it in the interface or cast the return value to a float.

如果在接口中没有声明返回值,编译器会将返回值视为id,因此您需要在接口中声明它,或者将返回值转换为浮点数。

#2


3  

I used your code but didn't find any issues. It is working fine for me. Just clean build your app once. Above code will work fine until h = 0.0005 if its beyond the value 0.0005 thats is 0.00005 or more, then you will get infinite value. So please check for it and do the necessary changes like using double/long instead.

我用了你的代码,但没有发现任何问题。对我来说还行。只需要清理一次你的应用。以上代码将一直工作到h = 0.0005,如果超过0.0005的值是0.00005或更多,那么您将得到无限的值。所以请检查一下,并做一些必要的改变,比如使用double/long代替。

#3


0  

Your code works fine for me using this to call it:

你的代码对我来说很好用,用这个来称呼它:

UIColor *aColor = [UIColor colorWithRed:0.3 green:0.1 blue:0.5 alpha:1.0];
float theHue = [self getHue:aColor];
NSLog(@"Float: %f", theHue);

Returns:

返回:

2010-04-07 11:49:33.242 Floating[8888:207] getHue
2010-04-07 11:49:33.243 Floating[8888:207] getHue results: 0.750000
2010-04-07 11:49:33.243 Floating[8888:207] Float: 0.750000

#4


0  

This just happened to me. But as it turns out, i was referencing an instance method from within a class method which you shouldn't be doing.

这就发生在我身上。但事实证明,我是从一个类方法中引用实例方法的,这是不应该做的。

Basically what i had erroneously done was:

基本上我做错的是:

+(int) someMethod {
     // stuff
     [self otherMethod];
}


-(int) otherMethod {
    // do something
}

So do check (not in OPs case, but if you have a similar error) if that is the problem.

所以一定要检查(不是在项目操作的情况下,但是如果你有类似的错误),如果这就是问题所在。

#1


5  

Have you declared your method in the interface of your class? If so, did you per accident indicate a different return value (e.g. id)?

在类的接口中声明了方法吗?如果是,你是否每次意外都表示不同的返回值(例如id)?

If it is not declared in your interface the compiler will treat the return value as id, so you would either need to declare it in the interface or cast the return value to a float.

如果在接口中没有声明返回值,编译器会将返回值视为id,因此您需要在接口中声明它,或者将返回值转换为浮点数。

#2


3  

I used your code but didn't find any issues. It is working fine for me. Just clean build your app once. Above code will work fine until h = 0.0005 if its beyond the value 0.0005 thats is 0.00005 or more, then you will get infinite value. So please check for it and do the necessary changes like using double/long instead.

我用了你的代码,但没有发现任何问题。对我来说还行。只需要清理一次你的应用。以上代码将一直工作到h = 0.0005,如果超过0.0005的值是0.00005或更多,那么您将得到无限的值。所以请检查一下,并做一些必要的改变,比如使用double/long代替。

#3


0  

Your code works fine for me using this to call it:

你的代码对我来说很好用,用这个来称呼它:

UIColor *aColor = [UIColor colorWithRed:0.3 green:0.1 blue:0.5 alpha:1.0];
float theHue = [self getHue:aColor];
NSLog(@"Float: %f", theHue);

Returns:

返回:

2010-04-07 11:49:33.242 Floating[8888:207] getHue
2010-04-07 11:49:33.243 Floating[8888:207] getHue results: 0.750000
2010-04-07 11:49:33.243 Floating[8888:207] Float: 0.750000

#4


0  

This just happened to me. But as it turns out, i was referencing an instance method from within a class method which you shouldn't be doing.

这就发生在我身上。但事实证明,我是从一个类方法中引用实例方法的,这是不应该做的。

Basically what i had erroneously done was:

基本上我做错的是:

+(int) someMethod {
     // stuff
     [self otherMethod];
}


-(int) otherMethod {
    // do something
}

So do check (not in OPs case, but if you have a similar error) if that is the problem.

所以一定要检查(不是在项目操作的情况下,但是如果你有类似的错误),如果这就是问题所在。