首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIColor扩展

UIColor扩展
EN

Stack Overflow用户
提问于 2019-08-01 12:43:32
回答 1查看 165关注 0票数 0

我在objective中扩展了UIColor类,头文件如下所示:

代码语言:javascript
复制
@interface UIColor (ColorDecision)
+ (UIColor*) directOrLegacyWithColor;
+ (UIColor*) changeColor;
@end

执行情况:

代码语言:javascript
复制
#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

我收到编译器在下面一行的警告:“不兼容指针类型”将“UIColor*”从“类”分配给“*”。

代码语言:javascript
复制
color = LoginsManager.getSharedInstance.isDirect ? returenedColor : [UIColor self];

我知道问题与指针有多大的关系,但我不知道该怎么做。

编辑

由于manishharma93注释,我现在解决了第一个问题,当我想要使用指定的方法时,这些方法似乎是不可见的。

我在全局头文件中定义了颜色,如下所示:

代码语言:javascript
复制
#define Color_C7           [SharedUtility colorWithHexString:@"FF6A00"] // orange
#define Color_Direct      [Utility colorWithHexString:@"313d53"] // Color for direct

这是Utility中'colorWithHexString‘函数的实现:

代码语言:javascript
复制
+(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];
}

现在,当我想使用我的扩展函数时,例如: self.background = Color_C7。directOrLegacyWithColor 'directOrLegacyWithColor‘函数不可见

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-08-01 13:08:59

在您的方法中,将"self“改为"UIColor self”。

代码语言:javascript
复制
+ (UIColor *)directOrLegacyWithColor {
     UIColor *color;
     UIColor *returenedColor = self.changeColor;
     color = LoginsManager.getSharedInstance.isDirect ? returenedColor : [UIColor self];
     return color;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57309329

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档