讨厌问这个问题,因为似乎有很多其他相似的问题,但在经过大部分答案后我仍然无法看到我的错误。
我有两个VC,VC1包含从音乐服务获取的歌曲列表。 VC2包含从VC1中选取的歌曲列表; VC2基本上是一个播放列表。
我已经设置了我的实体名称和类来匹配:SCTrack。这是我的SCTrack.h:
@interface SCTrack : NSManagedObject
@property (strong, nonatomic) NSString* title;
@property (strong, nonatomic) NSString* stream_url;
SCTrack.m:
#import "SCTrack.h"
@implementation SCTrack
@dynamic title;
@dynamic stream_url;
-(instancetype) initWithDictionary: (NSDictionary*) SCTrackDict {
self = [self init];
if (self) {
self.title = SCTrackDict[@"title"];
self.stream_url = SCTrackDict[@"stream_url"];
}
return self;
}
+(NSMutableArray *) parseJSONData: (NSData *) JSONData {
NSError* error;
NSMutableArray* SCTrackArray = [NSMutableArray new];
NSArray *JSONArray= [NSJSONSerialization JSONObjectWithData:JSONData options:0 error: &error];
if ([JSONArray isKindOfClass:[NSArray class]]) {
for (NSDictionary* trackDict in JSONArray) {
SCTrack* trackObject = [[SCTrack alloc]initWithDictionary:trackDict];
[SCTrackArray addObject:trackObject];
}
}
return SCTrackArray;
}
@end
我的AppDelegate.m
UITabBarController* tabBar = (UITabBarController*) self.window.rootViewController;
SCTrackListVC *SCVC = (SCTrackListVC*) [[tabBar viewControllers]objectAtIndex:2];
SCVC.managedObjectContext = self.managedObjectContext;
return YES;
在我的VC1.m
中-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
self.selectedTrack = self.SCTrackList[indexPath.row];
self.selectedTrackRow = indexPath.row;
self.selectedTrack = [[SCTrack alloc]initWithEntity:[NSEntityDescription entityForName:@"SCTrack" inManagedObjectContext:self.managedObjectContext]insertIntoManagedObjectContext:self.managedObjectContext];
NSError* error;
NSManagedObjectContext *context = self.managedObjectContext;
if (![context save:&error]) {
NSLog(@"Error! %@", error);
}
//This is the other way I tried it _selectedTrack = [NSEntityDescription insertNewObjectForEntityForName:@"SCTrack" inManagedObjectContext:self.managedObjectContext];
在我的VC2.m
中-(NSFetchedResultsController*) fetchedResultController {
if (_fetchedResultController != nil) {
return _fetchedResultController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *SCTrackEntity = [NSEntityDescription entityForName:@"SCTrack" inManagedObjectContext:[self managedObjectContext]];
[fetchRequest setEntity:SCTrackEntity];
// NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES];
// [fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
_fetchedResultController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[self managedObjectContext] sectionNameKeyPath:nil cacheName:nil];
_fetchedResultController.delegate = self;
return _fetchedResultController;
}
这是我得到的错误: CoreData:错误:无法在NSManagedObject类上调用指定的初始化程序' SCTrack'
核心数据非常新。已经在这几个小时了。谢谢你指点我正确的方向。
答案 0 :(得分:0)
NSManaged Object的指定初始化程序是 initWithEntity:insertIntoManagedObjectContext:。 变化
SCTrack* trackObject = [[SCTrack alloc]initWithDictionary:trackDict];
到
SCTrack *trackObject = [[SCTrack alloc]initWithEntity:entity insertIntoManagedObjectContext:context];