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. 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];
}
You are trying to call a class method on an instance.
Notice the
+: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]:Try changing it to:
And that should set you straight.