How do I save a custom struct to NSUserDefaults?
struct Paging {
NSInteger currentPage;
NSInteger totalResults;
NSInteger resultsPerPage;
};
typedef struct Paging Paging;
NSUserDefaults *userInfo = [NSUserDefaults standardUserDefaults];
[userInfo setStruct:paging forKey:@"paging"];
[userInfo synchronize];
The above code produces a run-time warning:
*** -[NSUserDefaults setObject:forKey:]: Attempt to insert non-property value...
One way is to turn it into NSData
In this case your struct is pretty simple, so you might want to add a category to NSUserDefaults to save and restore it. I’d implement that by saving an NSDictionary with four NSNumbers. A bit more work, but it means your plist will be humanly readable.