0

我想存储两种类型的数据。首先是默认的不可变数据集(如怪物统计数据、物品数据库)其次是角色统计数据,用于在用户退出应用程序时存储数据。

我已经很少使用coredata(在资源文件夹上存储sqlite默认设置)进行学习了。但我想知道存储数据的最佳方式是考虑内存、速度和加密。

4

2 回答 2

0

对于大数据,我建议您使用 CoreData 或 Sqilte(您可能想查看一个名为 FMDB 的 sqlite 包装器。)

对于更安全的数据,例如统计分数,我建议您使用钥匙串(示例)。

对于没有安全的小数据,您可能会使用NSUserDefaults.

NSArray *stringsArray = [[NSArray alloc] arrayWithObjects: string1, string2, string3, nil];
[[NSUserDefaults standardUserDefaults] setObject:stringsArray forKey:@"MyStrings"];
[[NSUserDefaults standardUserDefaults] synchronize]
于 2013-02-16T05:46:32.240 回答
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
于 2013-02-16T05:57:54.190 回答