簡體   English   中英

嘗試將CMTime作為可轉換屬性存儲在核心數據上

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

我之前使用過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

CMTime是一個結構, NSValueTransformer只能與對象(指針)一起使用。

解決方法是將CMTime包裝在NSValue

@property (nonatomic, retain) NSValue *tempo;

並將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