27

I'm getting this warning when I'm trying to compare RGB components of two UIColors

In .h file, I declared this

 -(int) ColorDiff:(UIColor *) color1 :(UIColor *)color2;

In .m file

 - (int) ColorDiff:(UIColor *) color1 :(UIColor *)color2{
   ... //get RGB components from color1& color2
   // compute differences of red, green, and blue values
   CGFloat red   = red1   - red2;
   CGFloat green = green1 - green2;
   CGFloat blue  = blue1  - blue2;

  // return sum of squared differences
  return (abs(red) + abs(green) + abs(blue));
  }

And then in same .m file, I compare 2 UIColors like this

 int d= ColorDiff(C1,C2);// I got the warning right here.

I did research and people say I must include the header file. I did this but didn't help in my case. Why am I getting this error?

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
JHHoang
  • 653
  • 1
  • 8
  • 22

3 Answers3

22

It's because you defined your function as a instance method, not a function. There are two solutions.

One of which is this to change your method declaration to this:

int ColorDiff(UIColor *color1, UIColor *color2) {
    // colorDiff's implementation
}

Or, you can change your call to this:

int d = [self ColorDiff:C1:C2];
Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201
3

The declaration in your .h file doesn't match your implementation in your .m file.

if the implementation of your method in your .m looks like this:

 - (int) ColorDiffBetweenColorOne:(UIColor *) color1 AndColorTwo:(UIColor *)color2
{
    ... //get RGB components from color1& color2
    // compute differences of red, green, and blue values
    CGFloat red   = red1   - red2;
    CGFloat green = green1 - green2;
    CGFloat blue  = blue1  - blue2;

    // return sum of squared differences
    return (abs(red) + abs(green) + abs(blue));
}

than you should declare it like this in .h:

- (int) ColorDiffBetweenColorOne:(UIColor *) color1 AndColorTwo:(UIColor *)color2; 

and to call it from that same .m file, use:

int d = [self ColorDiffBetweenColorOne:C1 AndColorTwo:C2];
AtkinsonCM
  • 376
  • 1
  • 14
  • Thanks your great answer. That was a typo. As Richard mentioned above, I just changed [self ColorDiff:C1:C2]; – JHHoang Jan 24 '12 at 22:23
  • @JHHoang That'll definitely solve the issue, too. But you might still consider renaming your method to something more user friendly. Without keywords to distinguish your two arguments your method isn't as clear. Check out the documentation: (http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CodingGuidelines/Articles/NamingMethods.html#//apple_ref/doc/uid/20001282-BCIGIJJF) – AtkinsonCM Jan 24 '12 at 22:34
  • Thanks for your comment. That was my first draft. I'm always trying st and make sure it work well before I make it more user friendly. i know it's bad. Thanks again – JHHoang Jan 24 '12 at 22:42
2

There is a prototype missing in the h.file!

Patricia
  • 2,885
  • 2
  • 26
  • 32