简化 Core Data 工作的一些辅助方法。
通过在您的 Podfile 中添加以下行来下载 EasyData CocoaPod
pod 'EasyData', '~> 0.2'
并在您的 Podfile 所在目录运行
pod install
在项目中,将 EasyData 头文件导入您希望使用的类中
#import "EasyData.h"
创建一个 EasyData 实例来操作数据。以下示例中称为 'easyDataInstance'。
@property (strong, nonatomic) EasyData *easyDataInstance;
插入使用了 [insertObjectOfType: withValues:]
方法。这个方法将具有键值对的对象插入 Core Data 中,适用于给定的类名和数据对象。
示例
NSDictionary *objectToInsert = [[NSDictionary alloc] initWithObjectsAndKeys:@"The Great Gatsby", @"title", 180, @"numberOfPages", nil];
[self.easyDataInstance insertObjectOfType:Book withValues:objectToInsert];
检索使用了 [retrieveObjectOfType:(id) withAttribute:(id) equalTo:(id)]
方法。这个方法用于检索具有指定的值的字段的对象。
示例
Book *book = [self.easyDataInstance retrieveObjectOfType:Book withAttribute:@"title" equalTo:@"The Great Gatsby"];
更新使用了 [updateObjectOfType:(id) withAttribute:(id) equalTo:(id) withNewValues:(NSDictionary*)]
方法。这个方法检索具有指定值的字段的对象,并用提供的值替换 values
字典中的所有字段。返回更新后的对象。
示例
NSDictionary *newValues = [[NSDictionary alloc] initWithObjectsAndKeys: @"1984", @"title", 266, @"numberOfPages", nil];
Book *book = [self.easyDataInstance updateObjectOfType:Book withAttribute:@"title" equalTo:"The Great Gatsby" withNewValues:newValues];
删除单个对象使用了 [deleteObjectOfType:(id) withAttribute:(id) equalTo:(id)]
方法。这个方法删除具有指定值的字段的对象。返回删除的对象。
示例
Book *book = [self.easyDataInstance deleteObjectOfType:Book withAttribute:@"title" equalTo:@"1984"];
删除多个对象使用了 [deleteAllObjectsOfType:(id)]
方法。这个方法删除给定类中的所有对象。不返回任何内容。
示例
[self.easyDataInstance deleteAllObjectsOfType:Book];
本软件使用受MIT许可协议许可。