I have a small iPhone app that stores a list of objects. The user can add and remove objects, but this list will remain fairly small (most users would have 10-30 objects). NSUserDefaults
seems much easier to work with, but will sqlite3
be faster? With only 30 "records" will there be any noticeable difference?

- 6,014
- 5
- 44
- 74

- 42,348
- 18
- 73
- 86
-
Core Data is the best option for iPhone OS now. – respectTheCode Apr 26 '10 at 20:20
2 Answers
NSUserDefaults is for user preferences, usually basic objects like NSString or NSNumber. Sqlite, serializing a collection of objects in a property list, or Core Data are all valid options for storing user data such as model objects you created.
You're not going to see a speed difference, but it's still best to pick the correct mechanism for what you're doing. If it's just preferences then use NSUserDefaults, otherwise I would serialize your objects to a plist. If you're new to Cocoa I would avoid Core Data and even sqlite at first, to give yourself a chance to learn the basics first.

- 40,399
- 3
- 75
- 82
-
I am new to Cocoa but not database systems. Most of the tutorials I have read make it seem like there is a lot of overhead in sqlite statements. It seems like the standard is to read all of the data at launch and cache changes until the app is terminated. – respectTheCode May 30 '09 at 19:12
-
I am just going to continue using sqlite3. It seems like the more logical solution for this type of data. – respectTheCode May 31 '09 at 20:02
Try with NSCoding protocol. Declare your class to implement NSCoding protocol:
@interface Person : NSObject <NSCoding>
Previous line promises to implement the following methods:
-(id)initWithCoder:(NSCoder *)coder;
-(void)encodeWithCoder:(NSCoder *)coder;
Your methods should look something like:
-(void)encodeWithCoder:(NSCoder *)coder {
[super encodeWithCoder:coder];
[coder encodeObject:firstName forKey:@"firstName"];
[coder encodeObject:lastName forKey:@"lastName"];
}
-(id)initWithCoder:(NSCoder *)coder {
[super init];
firstName = [[coder decodeObjectForKey:@"firstName"] retain];
lastName = [[coder decodeObjectForKey:@"lastName"] retain];
return self;
}

- 1,053
- 1
- 12
- 24
-
1
-
2This is the answer for "How can I save objects to NSUserDefaults" and not discussing the benefits of the different possibilities. – Alex Cio Jan 30 '15 at 16:23