gpt4 book ai didi

ios - 返回 uicolors 数组的类别

转载 作者:行者123 更新时间:2023-12-01 17:30:12 26 4
gpt4 key购买 nike

我有以下类别扩展 UIColor :

.h

#import <UIKit/UIKit.h>

@interface UIColor (Colors)
- (NSArray *)reds;
@end

.m
#import "UIColor+Colors.h"

@implementation UIColor (Colors)

- (NSArray *)reds {
return [[NSArray alloc] initWithObjects:[UIColor colorWithRed:1.0/255.0 green:50.0/255.0 blue:98.0/255.0 alpha:1],[UIColor colorWithRed:1.0/255.0f green:37.0f/255.0f blue:75.0f/255.0f alpha:1],[UIColor colorWithRed:1.0/255.0 green:1.0/255.0 blue:52.0/255.0 alpha:1],[UIColor colorWithRed:90.0/255.0 green:13.0/255.0 blue:1.0/255.0 alpha:1],[UIColor colorWithRed:53.0/255.0 green:6.0/255.0 blue:1.0/255.0 alpha:1], nil];
}

@end

然后在 View Controller 中,我试图做这样的事情: [cell setBackgroundColor:[UIColor reds][0]];知道我做错了什么吗?

最佳答案

我认为最可能的问题是函数 reds必须是类方法才能调用 reds在类里面。所以是这样的:

。H

@interface UIColor (Colors)
+(NSArray *)reds;
@end

.m
@implementation UIColor (Colors)

+(NSArray *)reds {
return @[[UIColor colorWithRed:1.0/255.0 green:50.0/255.0 blue:98.0/255.0 alpha:1],
[UIColor colorWithRed:1.0/255.0f green:37.0f/255.0f blue:75.0f/255.0f alpha:1],
[UIColor colorWithRed:1.0/255.0 green:1.0/255.0 blue:52.0/255.0 alpha:1],
[UIColor colorWithRed:90.0/255.0 green:13.0/255.0 blue:1.0/255.0 alpha:1],
[UIColor colorWithRed:53.0/255.0 green:6.0/255.0 blue:1.0/255.0 alpha:1]];
}

@end

所以基本上是 +而不是 - .如果您使用 -它是一个实例方法,因此您必须实例化 UIColor 的实例才能调用 reds .

关于ios - 返回 uicolors 数组的类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27789731/

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