Search code examples
iosobjective-cuikituicolor

Store UIColor in Array


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


Solution

  • 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.