Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSKeyedArchiver/NSuserDefaults - saving a CGRect

My app involves a user being able to drag specific items such as volume sliders and UIImage views. My goal is to be able to save the locations of the items so that the user can not have to drag the items again after re-launch. Unfortunately, I have seen errors along taking the 'saving CGRect rect = item.frame to NSuserDefaultsorNSKeyedArchiver' route. Any other suggestions?

like image 290
Flafla2 Avatar asked Feb 16 '10 03:02

Flafla2


2 Answers

You could use NSStringFromgCGRect() and CGRectFromString() to store them.

like image 80
Ben Gottlieb Avatar answered Sep 19 '22 01:09

Ben Gottlieb


If you're using keyed archiving on iPhone, UIKit specifically supports an addition that looks like this: -encodeCGRect:forKey: and does precisely what you want. See the Apple docs for NSCoder's UIKit additions.

If you're using NSUserDefaults, you don't get the luxury of using an explicit encoder for CGRects, but as some other answers say, you could save them as strings using NSStringFromCGRect() and its reverse.

like image 36
Ben Zotto Avatar answered Sep 21 '22 01:09

Ben Zotto