美文网首页
YTKKeyValueStore使用,不同控制器使用

YTKKeyValueStore使用,不同控制器使用

作者: Flynn_Lee | 来源:发表于2021-03-08 20:48 被阅读0次

    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;
    }
    

    相关文章

      网友评论

          本文标题:YTKKeyValueStore使用,不同控制器使用

          本文链接:https://www.haomeiwen.com/subject/gwdmqltx.html