0

我之前使用过 Core Data 的可转换属性,但没有C使用CMTime.

我需要CMTime使用可转换存储在核心数据上。

在 Model 对象上,我已将其声明为可转换的。

@property (nonatomic, retain) id tempo;

我知道这CMTime是一个C对象,我需要将其转换为 NSDictionary 并将其序列化,以便可以存储在 Core Data 上。

NSManagedObject课堂上,我有这个...

+(Class)transformedValueClass
{
  return [NSData class];
}

+(BOOL) allowsReverseTransformation
{
  return YES;
}

-(id)transformedValue:(id) value
{
  CFDictionaryRef dictTime = CMTimeCopyAsDictionary(?????, kCFAllocatorDefault);
  NSDictionary *dictionaryObject = [NSDictionary dictionaryWithDictionary:(__bridge NSDictionary * _Nonnull)(dictTime)];
  return [NSKeyedArchiver archivedDataWithRootObject:dictionaryObject];
}

-(id)reverseTransformedValue:(id)value
{
  // not complete ???
  return (NSDictionary*) [NSKeyedUnarchiver unarchiveObjectWithData:value];
}

?????是我的问题。你看,该方法transformedValue:(id)value接收值,id但我需要一个CMTime. 我不能简单地施放它。

此外,当我创建该实体的实例时,理论上我应该能够将值分配为:

myEntity.tempo = myCmTimeValue;

而且我没有看到我怎么能做到这一点id...

另外,在reverseTransformedValue:我返回一个id.

我不明白这个。我希望能够设置并接收CMTime

4

1 回答 1

1

CMTime是一个结构,NSValueTransformer只能与对象(指针)一起使用。

一种解决方法是换CMTimeNSValue

@property (nonatomic, retain) NSValue *tempo;

并转换NSValueCMTime,反之亦然

CMTime time = [self.tempo CMTimeValue];


self.tempo = [NSValue valueWithCMTime:time];
于 2018-03-10T18:56:17.663 回答