1、集成和導(dǎo)入可以查看此處:https://github.com/yuantiku/YTKKeyValueStore
2阐滩、數(shù)據(jù)存儲(chǔ)
YTKKeyValueStore *store = [[YTKKeyValueStore alloc] initDBWithName:@"flynn.db"];
NSString *tableName = @"user_table";
[store createTableWithName:tableName];
// 保存
NSString *key = @"1";
NSDictionary *user = @{@"id": @1, @"name": @"flynn", @"age": @30};
[store putObject:user withId:key intoTable:tableName];
// 查詢
NSDictionary *queryUser = [store getObjectById:key fromTable:tableName];
NSLog(@"query data result: %@", queryUser);
3二打、在不同控制器查看存儲(chǔ)的數(shù)據(jù)(可以在任意界面,此處是沒(méi)有寫(xiě)其他頁(yè)面掂榔,所以放在AppDelegate)
#import "AppDelegate.h"
#import "YTKKeyValueStore.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSString *pathName = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *filePath = [pathName stringByAppendingPathComponent:@"flynn.db"];
YTKKeyValueStore *store = [[YTKKeyValueStore alloc] initWithDBWithPath:filePath];
NSString *tableName = @"user_table";
NSString *key = @"1";//tableName和key可以做一個(gè)全局的宏继效,這樣不容易寫(xiě)錯(cuò)
NSDictionary *queryUser = [store getObjectById:key fromTable:tableName];
NSLog(@"其他頁(yè)面打印存儲(chǔ)結(jié)果: %@", queryUser);
return YES;
}
4、打印結(jié)果
2021-03-08 20:44:47.017347+0800 ytkKeyValueStore[7082:348678] 其他頁(yè)面打印存儲(chǔ)結(jié)果: {
age = 30;
id = 1;
name = flynn;
}