Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save list of CGPoints using NSUserDefaults

I have a bunch of CGPoints from a CCTMXLayer I want to save to NSUserDefaults but cannot seem to figure out an elegant way of doing so.

Originally I was hoping to save an NSDictionary with an NSMutableSet for a value containing several NSValues (valueWithCGPoint). From what I can tell neither NSMutableSet or NSValue is supported by NSUserDefaults.

At this point I am considering saving the x&y values of the CGPoint as a string and just converting it back and forth as needed.

like image 491
Chris Daviduik Avatar asked Jan 01 '11 21:01

Chris Daviduik


2 Answers

There exists a pair of functions NSStringFromCGPoint and CGPointFromString. You can use these to produce an array of strings representing the points for serialization, and then convert back when you're finished.

like image 81
Lily Ballard Avatar answered Oct 31 '22 12:10

Lily Ballard


Box them in NSValue objects. Once they're in there, you can write the NSValues out to disk (since they conform to <NSCoding>) or put them in NSUserDefaults or whatever.

like image 31
Dave DeLong Avatar answered Oct 31 '22 12:10

Dave DeLong