NSUserDefaults vs sqlite3

折月煮酒 提交于 2019-12-01 09:09:43

问题


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?


回答1:


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.




回答2:


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;
}


来源:https://stackoverflow.com/questions/930239/nsuserdefaults-vs-sqlite3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!