gpt4 book ai didi

objective-c - 在 NSDictionary 中存储 UIColors 并检索它们?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:06:25 25 4
gpt4 key购买 nike

我需要为给定索引返回特定的 UIColor。
我试图基本上将 UIColors 存储为 NSArrays

TypeColors = [[NSDictionary alloc] initWithObjectsAndKeys:

@"1", [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.5],[NSNumber numberWithFloat:0.5],[NSNumber numberWithFloat:0.5],[NSNumber numberWithFloat:1.0], nil],

@"5", [NSArray arrayWithObjects:[NSNumber numberWithFloat:1.0],[NSNumber numberWithFloat:0.5],[NSNumber numberWithFloat:0.1],[NSNumber numberWithFloat:1.0], nil] , nil]; //nil to signify end of objects and keys.

在这里我想从那个字典中取回 UIColor:

a = 5;
NSArray* colorArray = [TypeColors objectForKey:a];
UIColor* color = [UIColor colorWithRed:[colorArray objectAtIndex:0]
green:[colorArray objectAtIndex:1] blue:[colorArray objectAtIndex:2]
alpha:[colorArray objectAtIndex:3]];

它总是返回零,有人知道为什么吗?谢谢!

最佳答案

改成

UIColor* color = [UIColor colorWithRed:[[colorArray objectAtIndex:0] floatValue]
green:[[colorArray objectAtIndex:1] floatValue] blue:[[colorArray objectAtIndex:2] floatValue]
alpha:[[colorArray objectAtIndex:3] floatValue]];

要发送到那里的参数是cgfloat而不是NSNumber

关于objective-c - 在 NSDictionary 中存储 UIColors 并检索它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12677907/

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