1

There are plenty examples in the wild on how to bind instance methods in objc category, but I have yet to find how would one bind class method. For example if I have this category:

@interface UIColor (Awesome)
+ (UIColor *)colorFromHex:(NSString *)hex;
@end

And if I try to bind it like this:

[Category, BaseType(typeof(UIColor))]
interface UIColor_Awesome
{
    [Static, Export ("colorFromHex:")]
    UIColor ColorFromHex(string hex);
}

I'll get this method after Xamarin's code generation magic happens:

public static UIColor ColorFromHex (this UIColor This, string hex)

And I have no idea what to do with first arg - I don't have an instance to call this method on. How should I correctly write APIDefinition for this situation or how should I use generated method?

Thank you

Emil
  • 137
  • 1
  • 12

2 Answers2

0

Maybe this helps another person.

EDITED:

I had the same problem, I was binding a method in category.

Then, I found a solution posted here that just add the method to the constructor of the "base class".

In my case, I had a category of a UIView that is equivalent to the question. You just must remove the word "category".

Following the Emil's question:

[BaseType(typeof(UIColor))]
interface UIColor_Awesome
{
    [Static, Export ("colorFromHex:")]
    UIColor ColorFromHex(string hex);
}
debiasej
  • 980
  • 1
  • 14
  • 26
-1

That is an extension method. They allow you to extend / augment a class without having to subclass it or without having the code for it. This will be the instance of the class they they are called on.

var color = new UIColor(); // or some reference to a UIColor
var newColor = color.ColorFromHex("#FF0000");

or you can use it just like a static method (even though this is not how they are intended):

var color = new UIColor();
var newColor = UIColor.ColorFromHex(color, "#FF0000");

Here is another question about getting the hex color from UIColor.

*Disclaimer, I didn't compile the code.

Community
  • 1
  • 1
valdetero
  • 4,624
  • 1
  • 31
  • 46
  • So the solution is to provide a stub instance? – Emil Jun 16 '15 at 14:11
  • Is your main question how to use the extension method or are you really just wanting to know how to convert a hex to ui color? – valdetero Jun 16 '15 at 14:13
  • Main question is about how to bind a class method in category, colors were used as an example. Just in case - class methods don't have an instance to work with – Emil Jun 16 '15 at 14:14
  • You would want to provide the instance of whatever class you want to perform the method against. – valdetero Jun 16 '15 at 14:18
  • That's not always true and example above proves that. I don't need any intermediate instances of color to create new color from string, in objc it will be done like this: `UIColor *newColor = [UIColor colorFromHex:@"0xff0000"];` – Emil Jun 16 '15 at 14:23
  • It is for the extension method that you said it generates. Btw, what is generating it? – valdetero Jun 16 '15 at 15:03
  • Added more details in question - Xamarin generates this class extension, based on entry in APIDefinition. – Emil Jun 16 '15 at 15:50