views:

84

answers:

2

I'm puzzled... I have this function "colorWithHexString"... when I include it in the viewcontroller that's calling it then it works fine. But when I move it to a separate "BSJax" class and call it with the same input parameter it throws an unrecognized selector error. Here's the call:

BSjax *bsjax = [BSjax new];
NSString *hexString = [NSString stringWithString:@"CCCCFF"];
[self.view setBackgroundColor:[bsjax colorWithHexString:hexString]];

I'm pretty sure there's something about the way I'm calling the function that prevents it from working as a bsjax method. Probably some really dumb noob mistake that'll jump right out to experienced objective-c programmers... at least that's what I'm hoping. Any feedback will be appreciated.

BSjax.h includes:

+ (UIColor *)colorWithHexString:(NSString *)stringToConvert;

... and BSjax.m includes:

+ (UIColor *)colorWithHexString:(NSString *)stringToConvert
{
    NSString *cString = [[stringToConvert stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];

    // String should be 6 or 8 characters
    if ([cString length] < 6) NSLog(@"colorWithHexString called with parameter < 6 characters in length");

    // strip 0X if it appears
    if ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2];

    if ([cString length] != 6) NSLog(@"colorWithHexString called with parameter != 6 characters in length");

    // Separate into r, g, b substrings
    NSRange range;
    range.location = 0;
    range.length = 2;
    NSString *rString = [cString substringWithRange:range];

    range.location = 2;
    NSString *gString = [cString substringWithRange:range];

    range.location = 4;
    NSString *bString = [cString substringWithRange:range];

    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];

    return [UIColor colorWithRed:((float) r / 255.0f)
                           green:((float) g / 255.0f)
                            blue:((float) b / 255.0f)
                           alpha:1.0f];
}
+2  A: 

Is colorWithHexString declared in @interface BSjax in a header, and did you #import that header into the source file where the error is reported?

Edit:

+ (UIColor *)colorWithHexString:(NSString *)stringToConvert;

The above code (the +) declares a class method, meaning it should be called with the class name. You are calling it with an instance of the class, for which it is not defined. Try:

[self.view setBackgroundColor:[BSjax colorWithHexString:hexString]];
drawnonward
yes on both counts
codemonkey
+6  A: 

You are trying to call a class method on an instance.

Notice the +:

+ (UIColor *)colorWithHexString:(NSString *)stringToConvert;

It means you can only call the method as [ClassName classmethod]

And then here you are trying to use the method with an instance [instanceObject classmethod]:

BSjax *bsjax = [BSjax new];
[self.view setBackgroundColor:[bsjax colorWithHexString:hexString]];

Try changing it to:

[self.view setBackgroundColor:[BSjax colorWithHexString:hexString]];

And that should set you straight.

ohhorob
codemonkey