gpt4 book ai didi

ios - UI颜色扩展

转载 作者:行者123 更新时间:2023-11-28 23:29:24 25 4
gpt4 key购买 nike

我在 objective-c 中扩展了 UIColor 类头文件是这样的:

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

实现:

#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

我在下面的行“Incompatible pointer types assigning to 'UIColor *' from 'Class'”处收到来自编译器的警告

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

我知道问题与指针有某种关系,但我不知道该怎么办。

编辑

感谢 manishharma93 的评论,我现在解决了第一个问题,当我想使用指定的方法时,似乎这些方法不可见。

我在这样的全局头文件中定义了我的颜色:

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

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

+(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。直接OrLegacyWithColor“directOrLegacyWithColor”函数不可见

最佳答案

在您的方法中,将“self”更改为“[UIColor self]”。

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

关于ios - UI颜色扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57309329/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com