MagicalRecords 使用 NSSet 添加记录
MagicalRecords adding records with NSSet
我只是说要学习 CoreData 和 MR。我正在使用 Ray Wenderlich 的 BeerTracker 教程,但在向空数据库中添加记录时遇到问题。
// beer.h
@class BeerDetails;
@interface Beer : NSManagedObject
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) BeerDetails *beerDetails;
@end
//beerdetails.h:
@interface BeerDetails : NSManagedObject
@property (nonatomic, retain) NSString * image;
@property (nonatomic, retain) NSString * note;
@property (nonatomic, retain) NSNumber * rating;
@property (nonatomic, retain) NSManagedObject *beer;
// where data is being added to the tables:
Beer *paleLager = [Beer createEntity];
paleLager.name = @"Pale Lager";
paleLager.beerDetails = [BeerDetails createEntity];
paleLager.beerDetails.rating = @3;
我的table有一对多,所以它使用NSSet:
@属性(可为空,非原子,保留)NSSet *cells;
它似乎在主 table 上工作,但后来我设置了关系,就像示例中一样:(Section is one Cell is many)
Section *tempSection = [Section MR_createEntity];
tempSection.button = subButton;
tempSection.cells = [Cell MR_createEntity]; << Warning Here
从 'Cell * _Nullable'
分配给 'NSSet * _Nullable' 的不兼容指针类型
如果我将其更改为 1 对 1 关系,它似乎可以工作。令我困惑的部分是 NSSet *cells.
我找不到任何使用 NSSet 并手动将记录加载到文件中的示例。
看起来我在正常添加记录时不需要对 NSSet 做任何特殊的事情,只有在像 BeerTracker 那样添加它们时。我猜 CoreData 正在寻找指向 NSSet 对象的指针,但我不知道如何在这一行中设置它:
tempSection.cells = [Cell MR_createEntity];
感谢@sschale,这帮助我找到了正确的方向。
为了让其他人受益,剩下的是:
我用记录值创建了一个字典并修改了核心数据方法:
// added "with:(NSDictionary *) inputData;
- (void)addCells:(NSSet<Cell *> *)values with:(NSDictionary *) inputData;
// this call create the entity and passes the dictionary of keys/values
[tempSection addCells:(NSSet *)[Cell MR_createEntity] with:myDictionary];
// here's an example of changing the values inside the 'addCells:with:' method
[values setValue:[inputData objectForKey:@"overpic"] forKey:@"overpic"];
[values setValue:[inputData objectForKey:@"pictitle"] forKey:@"pictitle"];
[values setValue:[inputData objectForKey:@"position"] forKey:@"position"];
我不知道这是否是最好的方法,但到目前为止它似乎有效。 运行 这篇关于性能的文章可能有人感兴趣:
http://www.cocoawithlove.com/2009/11/performance-tests-replacing-core-data.html
以下是在 Objective C 中使用单个对象创建集合的语法:
tempSection.cells = [NSSet setWithObject:[Cell MR_createEntity]];
要处理多个项目,请使用:
tempSection.cells = [NSSet setWithObjects:[Cell MR_createEntity], [Cell MR_createEntity], ..., nil];
更常见的是,您想使用在 +CoreDataProperties.h
文件中为您创建的访问器:
- (void)addCellsObject:(Cell *)value;
- (void)removeCellsObject:(Cell *)value;
- (void)addCells:(NSSet<Cell *> *)values;
- (void)removeCells:(NSSet<Cell *> *)values;
所以在这种情况下,您可以调用:
[tempSection addCellsObject:[Cell MR_createEntity]];
我只是说要学习 CoreData 和 MR。我正在使用 Ray Wenderlich 的 BeerTracker 教程,但在向空数据库中添加记录时遇到问题。
// beer.h
@class BeerDetails;
@interface Beer : NSManagedObject
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) BeerDetails *beerDetails;
@end
//beerdetails.h:
@interface BeerDetails : NSManagedObject
@property (nonatomic, retain) NSString * image;
@property (nonatomic, retain) NSString * note;
@property (nonatomic, retain) NSNumber * rating;
@property (nonatomic, retain) NSManagedObject *beer;
// where data is being added to the tables:
Beer *paleLager = [Beer createEntity];
paleLager.name = @"Pale Lager";
paleLager.beerDetails = [BeerDetails createEntity];
paleLager.beerDetails.rating = @3;
我的table有一对多,所以它使用NSSet:
@属性(可为空,非原子,保留)NSSet *cells;
它似乎在主 table 上工作,但后来我设置了关系,就像示例中一样:(Section is one Cell is many)
Section *tempSection = [Section MR_createEntity];
tempSection.button = subButton;
tempSection.cells = [Cell MR_createEntity]; << Warning Here
从 'Cell * _Nullable'
分配给 'NSSet * _Nullable' 的不兼容指针类型如果我将其更改为 1 对 1 关系,它似乎可以工作。令我困惑的部分是 NSSet *cells.
我找不到任何使用 NSSet 并手动将记录加载到文件中的示例。
看起来我在正常添加记录时不需要对 NSSet 做任何特殊的事情,只有在像 BeerTracker 那样添加它们时。我猜 CoreData 正在寻找指向 NSSet 对象的指针,但我不知道如何在这一行中设置它:
tempSection.cells = [Cell MR_createEntity];
感谢@sschale,这帮助我找到了正确的方向。 为了让其他人受益,剩下的是:
我用记录值创建了一个字典并修改了核心数据方法:
// added "with:(NSDictionary *) inputData;
- (void)addCells:(NSSet<Cell *> *)values with:(NSDictionary *) inputData;
// this call create the entity and passes the dictionary of keys/values
[tempSection addCells:(NSSet *)[Cell MR_createEntity] with:myDictionary];
// here's an example of changing the values inside the 'addCells:with:' method
[values setValue:[inputData objectForKey:@"overpic"] forKey:@"overpic"];
[values setValue:[inputData objectForKey:@"pictitle"] forKey:@"pictitle"];
[values setValue:[inputData objectForKey:@"position"] forKey:@"position"];
我不知道这是否是最好的方法,但到目前为止它似乎有效。 运行 这篇关于性能的文章可能有人感兴趣: http://www.cocoawithlove.com/2009/11/performance-tests-replacing-core-data.html
以下是在 Objective C 中使用单个对象创建集合的语法:
tempSection.cells = [NSSet setWithObject:[Cell MR_createEntity]];
要处理多个项目,请使用:
tempSection.cells = [NSSet setWithObjects:[Cell MR_createEntity], [Cell MR_createEntity], ..., nil];
更常见的是,您想使用在 +CoreDataProperties.h
文件中为您创建的访问器:
- (void)addCellsObject:(Cell *)value;
- (void)removeCellsObject:(Cell *)value;
- (void)addCells:(NSSet<Cell *> *)values;
- (void)removeCells:(NSSet<Cell *> *)values;
所以在这种情况下,您可以调用:
[tempSection addCellsObject:[Cell MR_createEntity]];