Objective C -> Swift 2

I'm trying to convert the sample app AVMetadataRecordPlay from Objective C:


NSArray *specs = @[@{ (__bridge id)kCMMetadataFormatDescriptionMetadataSpecificationKey_Identifier : AVMetadataIdentifierQuickTimeMetadataLocationISO6709,
    (__bridge id)kCMMetadataFormatDescriptionMetadataSpecificationKey_DataType   : (__bridge id)kCMMetadataDataType_QuickTimeMetadataLocation_ISO6709 }];
CMFormatDescriptionRef locationMetadataDesc = NULL;
CMMetadataFormatDescriptionCreateWithMetadataSpecifications(kCFAllocatorDefault, kCMMetadataFormatType_Boxed, (__bridge CFArrayRef)specs, &locationMetadataDesc);


to Swift 2:


let specs = [[kCMMetadataFormatDescriptionMetadataSpecificationKey_Identifier as String: AVMetadataIdentifierQuickTimeMetadataLocationISO6709, kCMMetadataFormatDescriptionMetadataSpecificationKey_DataType as String: kCMMetadataDataType_QuickTimeMetadataLocation_ISO6709 as String]]
var locationMetadataDesc : CMFormatDescription
CMMetadataFormatDescriptionCreateWithMetadataSpecifications(kCFAllocatorDefault, kCMMetadataFormatType_Boxed, specs, &locationMetadataDesc)


Xcode 7.0 beta 2 reports error (not detailed enough):


Cannot invoke 'CMMetadataFormatDescriptionCreateWithMetadataSpecifications' with an argument list of type '(CFAllocator!, CMMetadataFormatType, [Dictionary<String, String>], inout CMFormatDescription)'


The function signature is


func CMMetadataFormatDescriptionCreateWithMetadataSpecifications(allocator: CFAllocator?, _ metadataType: CMMetadataFormatType, _ metadataSpecifications: CFArray, _ outDesc: UnsafeMutablePointer<Unmanaged<CMMetadataFormatDescription>?>) -> OSStatus


Any advice how to do this conversion properly?

Accepted Reply

Xcode 7 beta 3 changed the signature of CMMetadataFormatDescriptionCreateWithMetadataSpecifications(), so the correct conversion is


var locationMetadataDesc : CMMetadataFormatDescription?

Replies

Option click on CMMetadataFormatDescriptionCreateWithMetadataSpecifications and you'll see that the 4th arg doesn't match. Following the error message change

var locationMetadataDesc : CMFormatDescription


to this


var locationMetadataDesc : Unmanaged<CMMetadataFormatDescription>?


To use the value you'll need to use takeRetainedValue() or takeUnretainedValue (not sure which for that function) on the unwrapped optional.

Xcode 7 beta 3 changed the signature of CMMetadataFormatDescriptionCreateWithMetadataSpecifications(), so the correct conversion is


var locationMetadataDesc : CMMetadataFormatDescription?