我有NSURL
用于创建AVAsset
。然后,我使用AVAsset
从NSURL
表示的媒体文件中提取数据。在我开始使用Core Data
之前,这一直很好。要使其与Core Data
一起使用,我必须将NSURL
转换为NSString
作为entity
的属性。现在,当我提取时,我可以看到我能够检索NSURL
的字符串版本并将其转换回NSURL
。但是,当我从AVAsset
创建我的NSURL
时,我无法提取数据。
将NSURL存储为NSString方法:
-(void) saveRecording:(NSMutableDictionary*) recordPackage inManagedObjectContext:(NSManagedObjectContext*) context
{
Recording * recording = [NSEntityDescription insertNewObjectForEntityForName:@"Recording" inManagedObjectContext:context];
recording.audioURL = [[recordPackage objectForKey:@"audioURL"] absoluteString];
}
获取方法:
-(NSMutableArray*) fetchWithContext: (NSManagedObjectContext*) context
{
NSFetchRequest * request;
if(request == nil)
{
request = [NSFetchRequest fetchRequestWithEntityName:@"Recording"];
request.predicate = nil;
request.sortDescriptors = nil;
}
NSError * error;
return [[context executeFetchRequest:request error:&error] mutableCopy];
}
tableView的viewDidLoad:
NSMutableArray * testArray = [self.managedDocument fetchWithContext:self.managedDocument.managedObjectContext];
tableView的cellForRowAtIndexPath:
Recording * recording = [self.audioTableArray objectAtIndex:indexPath.row];
NSLog(@"THEURL %@", recording.audioURL);
NSURL * audioURL = [[NSURL alloc] initWithString:[recording.audioURL stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
AVAsset * transferredAsset = [AVAsset assetWithURL:audioURL];
[cell setAsset:transferredAsset];
当我NSLog
从NSString
获取Core Data
时,它与我存储之前的网址相匹配。是什么赋予了?我是否错误地在NSString
和NSURL
之间进行了转换?
我看到了以下答案,但我还是有点丢失:Storing an NSURL as a string in Core Data