我想存储两种类型的数据。 首先是默认的不可变数据集(如怪物统计数据,项目数据库) 其次是用户退出应用程序时存储数据的字符统计信息。
我已经很少使用coredata(在资源文件夹中存储sqlite默认设置)进行编程。但我想知道存储数据考虑内存,速度和加密的最佳方式是什么。
答案 0 :(得分:0)
对于大数据,我建议你使用CoreData或Sqilte(你可能想看一个名为FMDB的sqlite包装器。)
对于更安全的数据,例如统计分数,我建议您使用钥匙串(sample)。
对于没有安全的小数据,您可以使用NSUserDefaults
。
NSArray *stringsArray = [[NSArray alloc] arrayWithObjects: string1, string2, string3, nil];
[[NSUserDefaults standardUserDefaults] setObject:stringsArray forKey:@"MyStrings"];
[[NSUserDefaults standardUserDefaults] synchronize]
答案 1 :(得分:0)
我有点偏颇,但我更喜欢sqlite。我把我在一些项目中使用的一些代码示例鞭打在一起。如果您想查看其他一些代码示例,请与我们联系。
//
// SQLiteFunctions.h
//
#import <sqlite3.h>
#import "Users.h"
@interface SQLiteFunctions : NSObject
{
@public
@private
char *error;
sqlite3 *dbPointer;
sqlite3_stmt *sqlStatement;
}
@property (nonatomic, strong) Users *users;
@property (nonatomic, strong) NSMutableArray *returnData;
-(BOOL)openBundleDatabase:(int)bibleversion;
-(void)closeDatabase;
-(int)getUserRecords:(const char *)sqlquery;
@end
**
//
// SQLiteFunctions.m
//
#import "SQLiteFunctions.h"
@implementation SQLiteFunctions
@synthesize returnData;
@synthesize users;
-(BOOL)openBundleDatabase
{
// initializing db file access
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"dbname.sqlite"];
// testing to see if database was found
BOOL success = [fileMgr fileExistsAtPath:dbPath];
if(!success)
{
// can't find database
NSLog(@"Cannot locate database file '%@'.", dbPath);
return NO;
}
// testing for other error
if(!(sqlite3_open([dbPath UTF8String], &dbPointer) == SQLITE_OK))
{
// logging other error
NSLog(@"An error has occured: %s", sqlite3_errmsg(dbPointer));
return NO;
}
// returning success
return YES;
}
-(void)closeDatabase
{
// finalizing and closing the database
sqlite3_finalize(sqlStatement);
sqlite3_close(dbPointer);
}
-(int)getUserRecords:(const char *)sqlquery
{
// retreiving database values
if ([self getRecords:sqlquery])
{
// setting the date format
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MM/dd/yyyy"];
// looping through existing return rows
while (sqlite3_step(sqlStatement)==SQLITE_ROW)
{
// retreiving database values
users = [[Users alloc] init];
// retreiving database values
users.user_name = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 0)];
users.user_id = sqlite3_column_int(sqlStatement,1);
users.last_logged = [dateFormat dateFromString:[NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 2)]];
users.consecutive_days = sqlite3_column_int(sqlStatement,3);
users.audio = sqlite3_column_int(sqlStatement,4);
users.profile_skip = sqlite3_column_int(sqlStatement,5);
users.anim_speed = sqlite3_column_int(sqlStatement,6);
users.display_font = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 7)];
// adding object to the return data array
[returnData addObject:users];
}
}
// returning number of rows in the array
return [returnData count];
}
**
//
// Users.h
//
#import <Foundation/Foundation.h>
@interface Users : NSObject
@property (nonatomic,copy) NSString *user_name;
@property (nonatomic,assign) NSInteger user_id;
@property (nonatomic,copy) NSDate *last_logged;
@property (nonatomic,assign) NSInteger consecutive_days;
@property (nonatomic,assign) BOOL audio;
@property (nonatomic,assign) BOOL profile_skip;
@property (nonatomic,assign) NSInteger anim_speed;
@property (nonatomic,copy) NSString *display_font;
@end
**
//
// Users.m
//
#import "Users.h"
@implementation Users
@synthesize user_name;
@synthesize user_id;
@synthesize last_logged;
@synthesize consecutive_days;
@synthesize audio;
@synthesize profile_skip;
@synthesize anim_speed;
@synthesize display_font;
@end