//
//  ViewController.m
//  MakeStrong
//
//  Created by momingqi on 2019/7/15.
//  Copyright © 2019 momingqi. All rights reserved.
//

#import "ViewController.h"
#import <sqlite3.h>
#import <FMDB.h>
#import <YYModel.h>
#import <MMKV.h>

@interface User : NSObject
@property NSInteger uid;
@property NSString *name;
@property Float64 height;
@property User *subUser;
@end
@implementation User

- (NSString *)description
{
    return [self yy_modelDescription];
}

@end

@interface ViewController ()

@end


@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //学习文件存储
    //获取沙盒路径
    NSString *homeDir = NSHomeDirectory();
    NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSLog(@"home:%@", homeDir);
    NSLog(@"docDir:%@", docDir);

    //NSFileManager
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isDir;
    [fileManager fileExistsAtPath:[NSString stringWithFormat:@"%@/hello.txt", docDir] isDirectory:&isDir];
    if (isDir) {
        NSLog(@"hello.txt is a dir");
    } {
        NSLog(@"hello.txt not a dir");
    }

    //NSBundle 项目资源
    NSBundle *mainBundle = [NSBundle mainBundle];
    NSString *imgPath = [mainBundle pathForResource:@"xx" ofType:@"jpg"];
    NSLog(@"图片地址:%@", imgPath);

    //NSUserDefault 存储基础数据,自定义对象需要实现NSCoding;多线程安全
    [[NSUserDefaults standardUserDefaults] setInteger:3431 forKey:@"key_for_tt"];
    //手动同步
    BOOL synResult = [[NSUserDefaults standardUserDefaults] synchronize];
    NSLog(@"同步结果:%i", synResult);
    NSInteger result = [[NSUserDefaults standardUserDefaults] integerForKey:@"key_for_tt"];
    NSLog(@"读取userDefault:%d", (int)result);

    //SQLite3 C语言风格
    //创建/打开数据库
    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"test_db.sqlite"];
    NSLog(@"数据库路径:%@", path);
    sqlite3 *database;
    sqlite3_open([path UTF8String], &database);
    //建表
    const char *createSQL = "create table if not exists person(id integer primary key autoincrement, name char)";
    char *err;
    sqlite3_exec(database, createSQL, NULL, NULL, &err);
    //释放资源
    sqlite3_close(database);

    //FMDB
    FMDatabase *fmDB = [FMDatabase databaseWithPath:path];
    [fmDB open];
    //插入数据
    NSString *insertSQL = @"insert into person(name) values(?)";
    [fmDB executeUpdate:insertSQL, @"momingqi"];
    //查询数据 保证所有操作在同一队列进行
    FMDatabaseQueue *sqlQueue = [FMDatabaseQueue databaseQueueWithPath:path];
    //此方法是同步方法,会阻塞直到block被执行。
    [sqlQueue inDatabase:^(FMDatabase * _Nonnull db) {
        NSString *selectSQL = @"select * from person";
        FMResultSet *resultSet = [db executeQuery:selectSQL];
        while ([resultSet next]) {
            int value_id = [resultSet intForColumn:@"id"];
            NSString *name = [resultSet stringForColumn:@"name"];
            NSLog(@"查询结果:%d,%@", value_id, name);
        }
    }];
    [sqlQueue close];
    [fmDB close];

    //CoreData ORM操作
    //加载对象模型
//    NSString *modelPath = [[NSBundle mainBundle] pathForResource:@"UserModel" ofType:@"momd"];

    //NSCoding

    //YYModel 序列化工具 Runtime实现 安全类型转换 常见的Crash保护
    //JSON和对象互转
    NSString *json = @"{\"uid\":2717883, \"name\":\"momingqi\", \"height\":165.5}";
    User *user = [User yy_modelWithJSON:json];
    NSLog(@"转对象:%@", user);
    NSLog(@"转字符串:%@", [user yy_modelToJSONString]);
    //MMKV
    MMKV *mmkv = [MMKV defaultMMKV];
    int32_t firstInstall = [mmkv getUInt32ForKey:@"first_install"];
    NSLog(@"first_install=%d", firstInstall);
    [mmkv setInt32:-1 forKey:@"first_install"];
      
}
@end

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐