Take the 2-minute tour ×

I'm working on a text editor iPhone app where a user can type and save a note with custom font, size, and color. So I have an NSString, UIFont, and a UIColor. I need to store all these in some sort of data structure, but I'm not sure what would be the best method. Currently I'm using an NSMutableArray which writes to a plist for the string, and two other NSMutableArray of custom objects that save to NSUserDefaults on exit. But this is getting way too complicated to manage and debug. Is there any better way to do this?

share|improve this question

closed as not constructive by casperOne May 22 '12 at 14:18


This is how I'd save those items :) You say you're using arrays to store the font and color - surely for the font you could just save the name and just the hex code for the color? This would depend on how you currently load those and allow for selection, I guess. –  Luke Aug 28 '11 at 16:47

3 Answers 3

up vote 2 down vote accepted

I would create a custom NSObject subclass, called AttributedText. It would store these values using custom setters and could be retrieved from the data store with custom getters too. You could easily make it save to NSUserDefaults, if you used this custom object.

share|improve this answer

Your way is the most straightforward already. If you're looking for something a bit cleaner, you might consider building a small custom object that is responsible for:

  1. Storing the data you want, and
  2. Saving/loading in NSUserDefaults as needed

Define a simple getter/setter API on the object for the data you want, and use it to separate out the complexity of dealing with the arrays and user defaults. (You'll use most of the same implementation you already have, just in a different class.)

share|improve this answer
Would an NSDictionary be of any use here? –  moby Aug 28 '11 at 16:51
It depends entirely on what you're trying to do. It may be easier to use an NSDictionary with keys (text, font, color) than a three-element NSMutableArray, if that's your current implementation. –  Tim Aug 28 '11 at 20:43

I'd just store an attributed string.

share|improve this answer
NSAttributedString claims to conform to NSCoding on iOS, but it usually doesn't work since almost all of its attributes are non-bridged Core Text types. –  Wekwa Aug 29 '11 at 0:12
