要运行示例项目,请克隆仓库,然后首先从 "Example" 目录运行 pod install
。
您必须将 "libsqlite3.dylib" 添加到您的应用程序的 "链接框架和库" 部分中。
SnapIt
通过 SQLite 连接封装了将数据持久化到永久存储的常用模式。
#import "SnapIt.h"
@interface Cat : SnapIt
要持久化数据,从 SnapIt 类继承。
SAVE
方法Cat *mits = [[Cat alloc] init];
mits.name = @"Mits";
mits.color = @"orange";
[mits save];
要将对象添加到数据库中,修改其属性并运行保存方法。
ALL
方法NSArray *cats = [Cat all];
要从数据库检索所有对象,在您希望查询的类上运行 all 方法。
WHERE
方法NSArray *people = [Person where:@"name='Beth'"];
Person *beth = people[0];
要检索满足特定标准的对象,在对应的类上输入 where 子句,格式为 "object_property=value"。
DELETE
方法[lucy deleteSelf];
要从数据库中删除对象,在实例上运行 deleteSelf。
FETCH
方法[beth fetch];
要使用数据库中的值更新对象,在实例上运行 fetch 方法。在删除与类关联的对象后需要这样做。运行 fetch 以刷新数据。
BELONGS TO
关联@property (strong, nonatomic) Person *person;
要设置 belong to 关联,只需在头文件中按类名列出属性。属性名必须与类名相同,不要使用复数形式。(例如,Cat => "cat")
Person *beth = [[Person alloc] init];
beth.name = @"Beth";
[beth save];
Cat *bubbles = [[Cat alloc] init];
bubbles.name = @"Bubbles";
bubbles.color = @"grey";
bubbles.person = beth;
[bubbles save];
bubbles.person => <Person: 0x7ff323f33a20>
HAS MANY
关联@property (strong, nonatomic) NSArray *cats;
要设置 has many 关联,在头文件中按类名的复数形式列出数组属性。属性名必须与复数类名相同。(例如,Cat => "cats")
Person *beth = [[Person alloc] init];
NSArray *allCats = [Cat all];
beth.cats = allCats;
[beth save];
beth.cats => [
"<Cat: 0x7ff589f5ccc0>",
"<Cat: 0x7ff589f5d360>",
"<Cat: 0x7ff589f5dc70>",
"<Cat: 0x7ff589f5ead0>",
"<Cat: 0x7ff589f618a0>",
"<Cat: 0x7ff589f5f0f0>"
]
扎克尼亚兹,[email protected]
丹尼尔·吴,[email protected]
SnapIt可在MIT许可证下使用。更多信息请参阅LICENSE文件。