从Core Data获取NSURL的NSString版本

时间:2014-07-27 02:51:57

标签: ios cocoa-touch core-data avfoundation

我有NSURL用于创建AVAsset。然后,我使用AVAssetNSURL表示的媒体文件中提取数据。在我开始使用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];

当我NSLogNSString获取Core Data时,它与我存储之前的网址相匹配。是什么赋予了?我是否错误地在NSStringNSURL之间进行了转换?

我看到了以下答案,但我还是有点丢失:Storing an NSURL as a string in Core Data

0 个答案:

没有答案