简体   繁体   English

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

[英]Trying to store CMTime as Transformable property on Core Data

I have used transformable properties of Core Data before but not with C objects like CMTime . 我之前使用过Core Data的可转换属性,但未使用C对象(例如CMTime

I need to store a CMTime on Core Data using transformable. 我需要使用可转换在核心数据上存储CMTime

On the Model object I have declared this as transformable. 在Model对象上,我已经声明了它是可转换的。

@property (nonatomic, retain) id tempo;

I understand that CMTime is a C object and I need to convert this to a NSDictionary and serialize it, so I can store on Core Data. 我知道CMTime是一个C对象,我需要将其转换为NSDictionary并对其进行序列化,以便可以存储在Core Data上。

On the NSManagedObject class, I have this... 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];
}

The ????? ????? is my problem. 是我的问题。 You see, the method transformedValue:(id)value receives value as id but I need a CMTime . 您会看到,方法transformedValue:(id)value接收值作为id但是我需要CMTime I cannot simply cast it. 我不能简单地抛弃它。

Also, when I create an instance of this entity, in theory I should be able to assign the value as this: 同样,当我创建该实体的实例时,从理论上讲,我应该能够将值分配为:

myEntity.tempo = myCmTimeValue;

and I am not seeing how I can do that to an id ... 而且我没有看到如何对id ...

also, on reverseTransformedValue: I am returning an id . 另外,在reverseTransformedValue:我正在返回一个id

I do not understand that. 我不明白这个。 I want to be able to set and receive a CMTime 我希望能够设置和接收CMTime

CMTime is a struct and NSValueTransformer can only be used with objects (pointers). CMTime是一个结构, NSValueTransformer只能与对象(指针)一起使用。

A workaround is to wrap CMTime in NSValue 解决方法是将CMTime包装在NSValue

@property (nonatomic, retain) NSValue *tempo;

and convert NSValue to CMTime and vice versa 并将NSValue转换为CMTime ,反之亦然

CMTime time = [self.tempo CMTimeValue];


self.tempo = [NSValue valueWithCMTime:time];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM