尝试将 CMTime 存储为核心数据上的可转换 属性

Trying to store CMTime as Transformable property on Core Data

我以前使用过 Core Data 的可转换属性,但没有使用 C 对象,例如 CMTime

我需要使用 transformable 在 Core Data 上存储一个 CMTime

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

@property (nonatomic, retain) id tempo;

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

NSManagedObject class 上,我有这个...

+(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

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

解决方法是将 CMTime 包装在 NSValue

@property (nonatomic, retain) NSValue *tempo;

并将NSValue转换为CMTime,反之亦然

CMTime time = [self.tempo CMTimeValue];


self.tempo = [NSValue valueWithCMTime:time];