我正在尝试将视频的网址保存到核心数据。为此,我创建一个包含URL的字符串,将其保存到核心数据,然后在另一个视图控制器中检索它。在其他View Controller中检索它时,我收到此错误:Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initWithString:relativeToURL:]: nil string parameter'
我已经知道保存的URL包含URL,但在接收时,它是nill。我该如何解决这个问题。
以下是保存网址的代码:
- (void) saveVideo {
NSManagedObjectContext *context = [self managedObjectContext];
TimeTravelDetails *timeTravelDetails = [NSEntityDescription insertNewObjectForEntityForName:@"TimeTravelDetails" inManagedObjectContext:context];
NSString *stringForSave = [self.videoURL absoluteString];
NSLog(@"String being saved: %@", stringForSave);
[timeTravelDetails setValue: stringForSave forKey:@"urlString"];
NSError *error = nil;
if (![self.managedObjectContext save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
} else {
NSLog(@"URL String saved");
NSLog(@"String being saved: %@", stringForSave);
}
//NSLog(@"%@", self.videoURL);
}
以下是我检索它:
- (void) getUrl {
NSManagedObjectContext *context = [self managedObjectContext];
TimeTravelDetails *timeTravelDetails = [NSEntityDescription insertNewObjectForEntityForName:@"TimeTravelDetails" inManagedObjectContext:context];
NSString *stringForURL = [[NSString alloc] init];
stringForURL = timeTravelDetails.urlString;
NSLog(@"The string has been recieved: %@", stringForURL);
self.finalURL = [[NSURL alloc] initWithString:stringForURL];
}
答案 0 :(得分:1)
在getUrl函数中,您再次创建一个新的核心数据对象(将新的obj插入数据库),而不是查询已保存的数据。 像下面这样的sg应该可以用于查询db:
NSManagedObjectContext *moc = [self managedObjectContext];
NSEntityDescription *entityDescription = [NSEntityDescription
entityForName:@"TimeTravelDetails" inManagedObjectContext:moc];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDescription];
NSError* error;
NSArray *saved_time_travel_details = [moc executeFetchRequest:request error:&error];