我正在使用 NSManagedObject 来保存一些值,但没有为此使用实体。因为我不想将其限制在某些实体上。我尝试使用 (Oist *)managedObject.entity,但不工作。如何使用这个 managedObject 来获取它是这样的:
Oist.h
@class File;
@interface Oist : NSManagedObject
@property (nonatomic, retain) NSSet *files;
@end
@interface Oist (CoreDataGeneratedAccessors)
- (void)addFilesObject:(File *)value;
File.h
@class Oist;
@interface File : NSManagedObject
@property (nonatomic, retain) Oist *gist;
@end
- (void)newManagedObjectWithClassName:(NSString *)className forRecords:(NSDictionary *)records
{
NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:className inManagedObjectContext:[[GPCoreDataController sharedInstance] backgroundManagedObjectContext]];
[records enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
[self setValue:obj forKey:key forManagedObject:newManagedObject];
}];
}
- (void)setValue:(id)value forKey:(NSString *)key forManagedObject:(NSManagedObject *)managedObject
{
File *fileEntity = [NSEntityDescription insertNewObjectForEntityForName:@"File" inManagedObjectContext:[[GPCoreDataController sharedInstance] backgroundManagedObjectContext]];
if ([key isEqualToString:@"files"])
{
NSDictionary *files = (NSDictionary *)value;
for (NSString *key in [files allKeys]) {
NSDictionary *file = [files valueForKey:key];
NSLog(@"%@", file[@"filename"]);
[fileEntity setValue:file[@"filename"] forKey:@"filename"];
[fileEntity setValue:file[@"type"] forKey:@"type"];
}
} else if ([key isEqualToString:@"id"])
{
if ([managedObject.entity.name isKindOfClass:[Oist class]]) {
//here I want to use Oist's method - (void)addFilesObject:(File *)value; how to do that?
}
[managedObject setValue:[NSNumber numberWithInt:[value integerValue]] forKey:@"gistID"];
}
}