0

For example, can Objective-C call a C function, which calls Objective-C?

(UIColor *) getUIColorWithRGB(int r, int g, int b) {
    return [UIColor colorWithRed: r / 255.0 green: g / 255.0 blue: b / 255.0 alpha: 1];
}

@implementation UIColorCollection

+(UIColor *) lightCyanColor {
    return getUIColorWithRGB(224, 255, 255);
}
nonopolarity
  • 146,324
  • 131
  • 460
  • 740
  • Possible duplicate of http://stackoverflow.com/questions/801976/mixing-c-functions-in-an-objective-c-class – Peter M Jun 01 '12 at 11:16
  • Yes. Call the C code as you normally would code a C call. To call back to Objective-C the C code must be in a .m or .mm file. – Hot Licks Jun 01 '12 at 11:16

1 Answers1

1

If talking about possibility, it is possible. Just remove the brackets in the return value of the C function:

UIColor * getUIColorWithRGB(int r, int g, int b) {
    return [UIColor colorWithRed: r / 255.0 green: g / 255.0 blue: b / 255.0 alpha: 1];
}

Not sure whether this is OK as a programming practice, though.

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
  • ha, the different syntax is driving me nuts... I think if it is RGB, then 3 numbers is much more readable than `withRed: Green: Blue:` syntax – nonopolarity Jun 01 '12 at 11:20
  • @動靜能量: I think you can use `#define` to create a macro for this. – nhahtdh Jun 01 '12 at 11:22
  • oh i see... that's true... I am just not used to macros since I haven't used it for about 16 years... since 1996 when I switched over to Perl – nonopolarity Jun 01 '12 at 11:34