Reputation: 22705
I have a UIColor created by [UIColor colorWithPatternImage:...];
and I want to store it in an NSArray.
I have a problem, however, as I have the following code for normal colors
NSString *NSStringFromUIColor(UIColor *color) {
//Creates a string with RGB values of the color
const CGFloat *components = CGColorGetComponents(color.CGColor);
return [NSString stringWithFormat:@"[%f, %f, %f, %f]",
components[0],
components[1],
components[2],
components[3]];
}
But the above crashes with my unconventional patternColorFromImage. So once again, how do I store this color in an NSArray etc?
Thanks
Upvotes: 0
Views: 4539
Reputation: 21
NSString* NSStringFromColor(UIColor* color) {
CGFloat r, g, b, a;
[color getRed:&r green:&g blue:&b alpha:&a];
return [NSString stringWithFormat:@"[%f, %f, %f, %f"], r, g, b, a];
}
Upvotes: 0
Reputation: 4271
In the expression [UIColor redColor]
, it is obvious to see that redColor
is a selector
.
Also, we can convert selectors to NSString
s and vice-versa with:
NSString colorSel = NSStringFromSelector(@selector(redColor));
SEL redSel = NSSelectorFromString(colorSel");
I'm on a windows PC right now so cannot say for sure whether it should be enclosed in @selector()
or not. But you can try them both out to see which one is the right way.
One you get the selector from string, you can set color with:
if ([UIColor respondsToSelector:redSel])
yourElement.backgroundColor = [UIColor performSelector:redSel];
This is a basic example with basic UIColor
s. You can use UIColorFromRGB
macros, or CGColor
s too.
Once you get an NSString
with your color, you can easily store them in an array.
Upvotes: -1
Reputation: 2097
You can't store C types into an NSArray. You could do this:
[NSArray arrayWithObjects: [NSNumber numberWithDouble: components[0]], [NSNumber numberWithDouble: components[1]], nil];
If there in fact are only two components. Here's a more generic version:
CGColorRef cgColor = [myColor CGColor];
NSUInteger numComponents = CGColorGetNumberOfComponents(cgColor);
CGFloat const *components = CGColorGetComponents(cgColor);
NSNumber *componentObjects[numComponents];
for (NSUInteger i = 0; i < numComponents; i++)
componentObjects[i] = [NSNumber numberWithDouble: components[i]];
NSArray *colorArray = [NSArray arrayWithObjects: componentObjects count: numComponents];
Upvotes: 0
Reputation: 62676
If you want to save parameterized colors in an array, wrap the values in NSNumbers (though, UIColor seems like a better alternative). In other words, either
CGFloat red = 0.5;
NSArray *colorParams = @[ [NSNumber numberWithFloat:red], // etc
or, better:
NSArray *color = [UIColor colorWithRed:red // etc
Either of these objects can be placed in another array:
NSArray *myArrayOfColors = @[ colorParams, color, // but watch out, we have two distinct
// representations of colors in this array
Upvotes: 0
Reputation: 89509
NSArray works with Objective-C objects, which the "(float)
" cast I see up there most definitely is not.
To be more specific, "float
", "int
", "char
" are C data types, CGFloat is a Core Graphics data structure (and also not an Objective C object).
If you want to save colors in an array, they need to be true "UIColor
" Objective-C objects.
If you want to save your CGFloats in a NSArray, you can do the answer in this related question.
Upvotes: 4