【问题标题】:Trying to store CMTime as Transformable property on Core Data尝试将 CMTime 存储为 Core Data 上的可转换属性
【发布时间】:2018-03-10 18:12:49
【问题描述】:

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

我需要使用可转换在 Core Data 上存储 CMTime

在模型对象上,我已将其声明为可变形。

@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

【问题讨论】:

    标签: ios objective-c core-data cmtime


    【解决方案1】:

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

    一种解决方法是将CMTime 包装在NSValue

    @property (nonatomic, retain) NSValue *tempo;
    

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

    CMTime time = [self.tempo CMTimeValue];
    
    
    self.tempo = [NSValue valueWithCMTime:time];
    

    【讨论】:

      猜你喜欢
      • 2018-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-05
      • 2023-03-28
      • 2022-12-08
      • 2018-05-24
      相关资源
      最近更新 更多