I'm currently attempting to add some video clip settings to a NSMutableDictionary, including two CMTime objects.
I am trying to store the video stream in use (indicated with an integer), the clip duration (CMTime) and the clip start time (CMTime), which are defined elsewhere in the code.
I'm probably being daft but I can't figure out how to add the CMTimes to the dictionary, I get a "Sending 'CMTime' to parameter of incompatible type 'id'" error.
I tried both setObject and setValue with no success and can't find an answer anywhere.
NSMutableDictionary *clipDetails = [NSMutableDictionary dictionary];
[clipDetails setObject:[NSNumber numberWithInteger:currentStream] forKey:@"stream"];
[clipDetails setObject:startTime forKey:@"clipStart"];
[clipDetails setObject:duration forKey:@"duration"];
Since CMTime
is a struct, you need to wrap it in an Objective C type, usually with NSValue
:
CMTime startTime = (...);
NSValue *startValue = [NSValue valueWithBytes:&startTime objCType:@encode(CMTime)];
[clipDetails setObject:startValue forKey:@"startTime"];
You can get it out again like so:
CMTime startTime;
NSValue *startValue = [clipDetails objectForKey:@"startTime"];
[startValue getValue:&startTime];
Sidenote, it's much easier to use the new dictionary syntax:
clipDetails[@"startTime"] = ...;
NSValue *value = clipDetails[@"startTime"];
Those steps will work for any struct; as it turns out, the AVFoundation framework provides convenience methods for CMTime structs:
clipDetails[@"startTime"] = [NSValue valueWithCMTime:startTime];
CMTime startTime = [clipDetails[@"startTime"] CMTimeValue];