YTKKeyValueStore使用,不同控制器使用

1、集成和导入可以查看此处:https://github.com/yuantiku/YTKKeyValueStore

2、数据存储

    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、在不同控制器查看存储的数据(可以在任意界面,此处是没有写其他页面,所以放在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可以做一个全局的宏,这样不容易写错
    NSDictionary *queryUser = [store getObjectById:key fromTable:tableName];
    NSLog(@"其他页面打印存储结果: %@", queryUser);
    return YES;
}

4、打印结果

2021-03-08 20:44:47.017347+0800 ytkKeyValueStore[7082:348678] 其他页面打印存储结果: {
    age = 30;
    id = 1;
    name = flynn;
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容