Search code examples
iosobjective-ciphoneuicolor

UIColor extension


I have extended UIColor class in objective-c the header file is like this:

@interface UIColor (ColorDecision)
+ (UIColor*) directOrLegacyWithColor;
+ (UIColor*) changeColor;
@end

implementation:

#import "UIColor+ColorDecision.h"

@implementation UIColor (ColorDecision)
 + (UIColor *)directOrLegacyWithColor {
     UIColor *color;
     UIColor *returenedColor = self.changeColor;
     color = LoginsManager.getSharedInstance.isDirect ? returenedColor : [UIColor self];
     return color;
}

 + (UIColor *)changeColor{
     UIColor *newColor;
     if (self == Color_C7) newColor = Color_Direct;

     return newColor;
}
@end

I get an warning from compiler at the below line 'Incompatible pointer types assigning to 'UIColor *' from 'Class''

color = LoginsManager.getSharedInstance.isDirect ? returenedColor : [UIColor self];

I know the problem is some how related to the pointers but I don't get what should I do.

EDIT

Thanks to manishharma93 comment I resolved the first problem now when I want to use the specified method it seems that the methods are not visible.

I defined my colors in a global header file like this:

#define Color_C7           [SharedUtility colorWithHexString:@"FF6A00"] // orange
#define Color_Direct      [Utility colorWithHexString:@"313d53"] // Color for direct

This is the implementation of the 'colorWithHexString' function in the Utility class:

+(UIColor *)colorWithHexString:(NSString *)hexString {
NSString *colorString = [[hexString stringByReplacingOccurrencesOfString: @"#" withString: @""] uppercaseString];
CGFloat alpha, red, blue, green;
switch ([colorString length]) {
    case 3: // #RGB
        alpha = 1.0f;
        red   = [SharedUtility colorComponentFrom: colorString start: 0 length: 1];
        green = [SharedUtility colorComponentFrom: colorString start: 1 length: 1];
        blue  = [SharedUtility colorComponentFrom: colorString start: 2 length: 1];
        break;
    case 4: // #ARGB
        alpha = [SharedUtility colorComponentFrom: colorString start: 0 length: 1];
        red   = [SharedUtility colorComponentFrom: colorString start: 1 length: 1];
        green = [SharedUtility colorComponentFrom: colorString start: 2 length: 1];
        blue  = [SharedUtility colorComponentFrom: colorString start: 3 length: 1];
        break;
    case 6: // #RRGGBB
        alpha = 1.0f;
        red   = [SharedUtility colorComponentFrom: colorString start: 0 length: 2];
        green = [SharedUtility colorComponentFrom: colorString start: 2 length: 2];
        blue  = [SharedUtility colorComponentFrom: colorString start: 4 length: 2];
        break;
    case 8: // #AARRGGBB
        alpha = [SharedUtility colorComponentFrom: colorString start: 0 length: 2];
        red   = [SharedUtility colorComponentFrom: colorString start: 2 length: 2];
        green = [SharedUtility colorComponentFrom: colorString start: 4 length: 2];
        blue  = [SharedUtility colorComponentFrom: colorString start: 6 length: 2];
        break;
    default:
        LOG(@"WARNING: tried to set color from string: %@ BUT string should be  a hex value of the form #RBG, #ARGB, #RRGGBB, or #AARRGGBB ", hexString);
        return nil;
        break;
}
return [UIColor colorWithRed: red green: green blue: blue alpha: alpha];
}

now when I want to use my extension function for example like this: self.background = Color_C7. directOrLegacyWithColor the 'directOrLegacyWithColor' function is not visible


Solution

  • In you method, Change "self" to "[UIColor self]".

    + (UIColor *)directOrLegacyWithColor {
         UIColor *color;
         UIColor *returenedColor = self.changeColor;
         color = LoginsManager.getSharedInstance.isDirect ? returenedColor : [UIColor self];
         return color;
    }