I’m newer to Xcode development and am trying to save the state of my app which tracks multiple index sets, integers and strings. I’ve tried a lot of different code and haven’t been able to get it to work saving to a .plist. What is the best approach for saving the following data types, NSMutableIndexSets and NSUIntegers? Any direction would be great, Thanks.
I’m newer to Xcode development and am trying to save the state of my
Share
The short answer to your question is that you can’t save index sets to a plist, or to user defaults. There is a very short list of object types that you can write to a plist. Look up the docs on the NSDictionary class in Xcode, and search for the string “property list object” That’s where they tell you which objects can be written to a properly list. The object types are NSString, NSData, NSDate, NSNumber, NSArray, or NSDictionary objects.
Omar Abdelhafith posted a very long and complex block of code to convert an index set to an array, which should work.
There is a much simpler way however. NSIndexSet conforms to the NSCoding protocol, which means that you can convert it to NSData with a single call:
And to turn it back into an index set:
Note that with all of these approaches, if you start with a mutable object (set, array, dictionary, etc) then when you read it back, the object you get back will be an immutable version. You have to manually convert it to a mutable version. Most objects that have a mutable variant support the method mutableCopy.