小型数据库的方法-iOS [英] Approach for small database - iOS
问题描述
我正在构建一个需要我存储很多东西的应用程序.
i am building an app that requires me to store a number of things.
我有一个由4到5个BOOL变量组成的数据对象,可能有800-1000个我需要保留的对象.
I have a data object consisting of 4 to 5 BOOL variables, there could be 800 - 1000 such objects which i will need to persist.
由于NSUserdefaults显然不是一种选择,我很困惑应该如何编程,应该使用sql数据库还是Core数据.
Am confused how should i program this, should i go for an sql database or Core data, since NSUserdefaults is not an option obviously.
推荐答案
您可以按照@adobels的建议轻松地将它们存储在plist中.您的类会将BOOL存储在NSNumber中,而您的类实现了NSCoding:
You can easily store them in a plist as @adobels suggested. Your Class would store the BOOLs in a NSNumber and your class implements NSCoding:
- (void)encodeWithCoder:(NSCoder *)coder {
[coder encodeObject:member1ToStore forKey:@"yourFirstBoolKey"];
}
- (id)initWithCoder:(NSCoder *)coder ...
如果您的类的所有实例都在NSArray(或类似的Cocoa集合)中,则只需将其归档并取消归档即可,例如
If all the instances of your class are in an NSArray (or similar Cocoa Collection) you then simply archive and unarchive to a file like
[NSKeyedArchiver archiveRootObject:yourCollectionOfClasses toFile:archivePath]
[NSKeyedUnarchiver unarchiveObjectWithFile:[[self archiveURL] path]];
请参见在苹果开发人员处的文档和<一个href ="https://stackoverflow.com/questions/8727508/ios-persistent-storage-strategy?answertab=active#tab-top">这个好答案
这篇关于小型数据库的方法-iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!