快速简单地使用 NanoStore 对象作为您的模型。
NanoStoreModel 由两部分组成:一个模型 DSL 和一层精薄的 Objective-C 类,它提供了动态属性访问器、初始化器、查找器、辅助工具和 KVO。
阶段:概念验证。请不要在生产中使用,但尝试一下看看它是否有效!
像为 NSMObject 的子类一样,通常定义属性。
@interface User : NSMObject
@property (strong) NSString* name;
@property (strong) NSNumber* age;
@property (strong) NSDate* createdAt;
@property (strong) NSFNanoBag* cars;
@end
@implementation User
@dynamic name, age, createdAt;
@end
// Instantiate a NanoStore and open it
NSFNanoStore *nanoStore = [NSFNanoStore createAndOpenStoreWithType:NSFMemoryStoreType path:nil error:nil];
// Create an User
User *user = [User model];
user.name = @"Joe";
user.age = @20;
user.createdAt = [NSDate date];
// Add it to the document store
[nanoStore addObject:object error:nil];
// Save the Store
[nanoStore saveStoreAndReturnError:nil];
// Close the document store
[nanoStore closeWithError:nil];
// Instantiate a NanoStore and open it
NSFNanoStore *nanoStore = [NSFNanoStore createAndOpenStoreWithType:NSFMemoryStoreType path:nil error:nil];
// Create an User
User *user = [User modelWithDictionary:@{@"name": @"Joe", @"age": @18, @"createdAt": [NSDate date]}];
// Add a bag
user.cars = [NSFNanoBag bag];
// Add object to bag
[user.cars addObject:[Car modelWithDictionary:@{@"name": @"Honda"}] error:nil];
// Add them to the document store
[nanoStore addObject:user error:nil];
[nanoStore addObject:user.cars error:nil];
// Save the Store
[nanoStore saveStoreAndReturnError:nil];
// Close the document store
[nanoStore closeWithError:nil];