1

Below is my NSMutableArray:

NSMutableArray *bgColorArr = [NSMutableArray arrayWithObjects:
    @"[UIColor colorWithRed:0.90 green:0.22 blue:0.21 alpha:1.0]",
    @"[UIColor colorWithRed:0.26 green:0.63 blue:0.28 alpha:1.0]",
    @"[UIColor colorWithRed:0.85 green:0.11 blue:0.38 alpha:1.0]",
    @"[UIColor colorWithRed:0.12 green:0.53 blue:0.90 alpha:1.0]",
    @"[UIColor colorWithRed:0.96 green:0.49 blue:0.00 alpha:1.0]",
    nil];

I am storing UIColors as strings due to some reason. I know how to store UIColors in NSMutableArray but I need to store them as NSString due to my requirement. When I am trying to display the colors in cellForRowAtIndexPath by using below code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *simpleTableIdentifier = .............

    cell.bgView.backgroundColor = (UIColor *)[bgColorArr objectAtIndex:indexPath.row];

    return cell;
}

Below is the error:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString CGColor]: unrecognized selector sent to instance

By looking at the above error we can say that the String is not converting to UIColor. Is there any way to display the UIColor even though stored as a String? Any help will be really appreciated.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Madhu
  • 439
  • 2
  • 14
  • You are trying to create an Array with String here. You better create an array with Hex code of colors and access them with the index. – MrWaqasAhmed May 02 '18 at 09:33
  • You are keeping `NSString`, why? Why don't you do `NSMutableArray *bgColorArr = [NSMutableArray arrayWithObjects:[UIColor colorWithRed:0.90 green:0.22 blue:0.21 alpha:1.0], [UIColor colorWithRed:0.26 green:0.63 blue:0.28 alpha:1.0],[UIColor colorWithRed:0.85 green:0.11 blue:0.38 alpha:1.0],[UIColor colorWithRed:0.12 green:0.53 blue:0.90 alpha:1.0], [UIColor colorWithRed:0.96 green:0.49 blue:0.00 alpha:1.0], nil];`? – Larme May 02 '18 at 09:33
  • You really need to explain why your colors are being stored as string representation of Objective-C code because that makes no sense at all. – rmaddy May 02 '18 at 14:57
  • It makes no sense to store Objective-C code in a string and then try to make use of that string later. – rmaddy May 02 '18 at 16:31
  • 1
    I'm asking why you are doing something so unusual so I can possibly provide a helpful answer. In none of the examples in your comment would you every put Objective-C code into a string. So why do it for UIColor? – rmaddy May 02 '18 at 16:40
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/170236/discussion-between-madhu-and-rmaddy). – Madhu May 02 '18 at 16:42
  • Sorry @rmaddy, my client is having a unique requirement and we came out with this solution. – Madhu May 02 '18 at 16:51
  • After seeing the newly added code this is just the worst. If you want the color to be dynamic store as a hex value string. Otherwise make it a UIColor – agibson007 May 02 '18 at 18:19
  • @rmaddy, we are sending the UIColors code from server since we need to change the Colors of UITableViewCells dynamically. So we are sending the UIColor through JSON as NSString. – Madhu May 03 '18 at 04:52
  • 1
    But you are sending Objective-C code as strings. There are MUCH better ways to represent colors as strings. Objective-C code in a string is way down at the very bottom of the list. A common string representation is a CSS3 RGB code in the format "#RRGGBB". The answer by "S George" below mentions this and links to code for this. – rmaddy May 03 '18 at 05:01
  • @rmaddy, thanks for suggesting a helpful answer. – Madhu May 03 '18 at 05:11

4 Answers4

2

Try this way.

Just include it in your header and it will available throughout your project.

#define RGBCOLOR(r,g,b)    [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1]

Then store UIColors in NSMutableArray like that:

NSMutableArray *colorArray=[[NSMutableArray alloc]initWithObjects:RGBCOLOR(107, 252, 0),RGBCOLOR(64, 128, 0),RGBCOLOR(51, 128, 0),RGBCOLOR(47, 128, 0),RGBCOLOR(31, 12, 155),RGBCOLOR(47, 72, 254),RGBCOLOR(86, 0, 255),RGBCOLOR(128, 24, 255),RGBCOLOR(240, 101, 255),RGBCOLOR(240, 45, 128),RGBCOLOR(129, 19, 0),RGBCOLOR(220, 18, 3),RGBCOLOR(236, 82, 9),RGBCOLOR(235, 146, 14),RGBCOLOR(255, 255, 0),RGBCOLOR(128, 64, 0),RGBCOLOR(50, 128, 63),RGBCOLOR(12, 64, 128),     RGBCOLOR(102, 102, 255),RGBCOLOR(115, 103, 61),nil];

Now display the colors in cellForRowAtIndexPath.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 static NSString *CellIdentifier = @"Cell";
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
 if (cell == nil) {
 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
 }

 cell.bgView.backgroundColor = [colorArray objectAtIndex:indexPath.row];

 return cell;
}

this will solve your problem.

Mukesh
  • 777
  • 7
  • 20
1

You can store hex strings in your array and then init UIColor with it.

Here are some links that you might find useful to work with hex strings.

How to use hex colour values

How can I create a UIColor from a hex string?

S. George
  • 36
  • 3
  • 1
    You should elaborate by showing this in action as you have the correct answer. I do this with an app and the colors are stored in a Json so I know this is right. – agibson007 May 02 '18 at 11:17
  • Left with no option and everyone is suggesting to use hex string so i am going with the above solution. – Madhu May 03 '18 at 05:07
  • @Madhu Keep in mind that using a language independent, standard format for your color strings from your server also makes your colors much easier to work with and you can write code in any language to parse the hex strings. – rmaddy May 03 '18 at 05:14
0

You can try storing rgb values and convert those string rgb values to float and place them in UIColor initialiser.

Vikas Dadheech
  • 1,672
  • 12
  • 23
0

You can add custom colors in your Asset.xcasset file like this.

enter image description here enter image description here

then load your colors like this in your array.

-(void)loadAllColorsFromAsset {
    NSMutableArray *array = [NSMutableArray arrayWithObjects:[UIColor colorNamed:@"redColor"],[UIColor colorNamed:@"yellowColor"],[UIColor colorNamed:@"greenColor"], nil];
}
Syed Qamar Abbas
  • 3,637
  • 1
  • 28
  • 52