0

I have set a color in an NSUserDefault in another class and and now want to convert it to a CGColor for display. I can convert an actual UIColor to a CGColor. But I'm having trouble converting a UIColor stored in a variable to a CGColor

UIColor* myColor = [[NSUserDefaults standardUserDefaults] objectForKey:myColor];
struct CGColor* circleColor = nil;
circleColor=[[UIColor greenColor]CGColor];//this works 
circleColor=[[myColor] CGColor];//does not work
circleColor=[myColor CGColor];//does not work

Can anyone suggest the right way to do this?

Note: I did not save a cgcolor in the userdefaults due to the need to bridge

user6631314
  • 1,751
  • 1
  • 13
  • 44
  • Have you tried `circleColor=[myColor CGColor];`? There shouldn't be square brackets around `myColor` – bachonk Mar 21 '18 at 20:40

3 Answers3

2

Replace

circleColor = [[myColor] CGColor];//does not work

with

circleColor = myColor.CGColor;

EDIT

If you haven't saved your UIColor in NSUSerDefaults check this answer for an example on how to correctly store and retrieve it.

LS_
  • 6,763
  • 9
  • 52
  • 88
  • You were right about syntax. However, I had second error in saving in NSUser Defaults. I upvoted you but marked other one correct as it was deeper issue. – user6631314 Mar 21 '18 at 20:54
  • It's Objective-C, that square bracket syntax is perfectly valid for properties – chedabob Mar 21 '18 at 22:13
2

You can't save UIColor object in NSUserDefaults directly.

try to archive object to get data and save the data like this:

UIColor *color = [UIColor redColor];
NSData *colorData = [NSKeyedArchiver archivedDataWithRootObject:color];
[[NSUserDefaults standardUserDefaults] setObject:colorData forKey:@"ColorKey"];

And when you need the color firstly you should get NSData object from User Defaults and then create UIColor object like this

NSData *colorData = [[NSUserDefaults standardUserDefaults] objectForKey:@"ColorKey"];
UIColor *color = [NSKeyedUnarchiver unarchiveObjectWithData:colorData];
Jabson
  • 1,585
  • 1
  • 12
  • 16
  • out of curiosity, I guess there is no way to save a string in the NSuserdefault as in @"greenColor" and use that to generate [UIColor greenColor]? – user6631314 Mar 21 '18 at 21:00
  • yes you can generate a new object. It depends on what are you doing. if you want to save a color generated by user and load it after some times. than user defaults is great to do such work. – Jabson Mar 21 '18 at 21:14
  • what would correct syntax be for NSString *colorstr = @"greencolor"; UIColor* myColor =[UIColor colorstr]; latter does not work.(stars don't show in comment) – user6631314 Mar 21 '18 at 21:25
  • when you type [UIColor greenColor] it means that you are calling UIColor class method "greenColor". it is impossible to type here variable declared earlier. like this [UIColor colorstr]; you will get compile error. – Jabson Mar 21 '18 at 21:29
  • So there is no way to simply generate standard color from string – user6631314 Mar 21 '18 at 21:32
  • from string no, but you can create UIColor category and add your own custom methods to UIColor class https://stackoverflow.com/questions/19397024/using-a-uicolor-category-in-ios-7-and-objective-c – Jabson Mar 21 '18 at 21:36
  • Ok. Got it . Thanks – user6631314 Mar 21 '18 at 21:36
1

I think you haven't create myColor object in this line.

UIColor* myColor = [[NSUserDefaults standardUserDefaults] objectForKey:myColor];
struct CGColor* circleColor = nil;

First initialise that object then assign on that object with your user defaults then proceed accordingly.

Mohammad Kanan
  • 4,452
  • 10
  • 23
  • 47
Abhishek Mitra
  • 3,335
  • 4
  • 25
  • 46