Secidentity + Force Cast Violation: Force Casts Should Be Avoided. (Force_Cast)

SecIdentity + Force Cast Violation: Force casts should be avoided. (force_cast)

Basically, what you're doing is a force downcast, meaning that you guarantee that yours identityPointer and trustPointer are objects of SecIdentity and SecTrust classes respectively. But what if they are not? You take them out of the dictionary as AnyObject, so generally they may not cast to the target classes. Swiftlint tells you, that force casting is a bad practise and wants you to avoid it.

However, it seems with CoreFoundation type you can't use a conditional cast as?, so force cast is your only choice. For this particular case you can disable Swiftlint rule in code adding a special comment.

let secIdentityRef = identityPointer as! SecIdentity // swiftlint:disable:this force_cast

To be on the safe side, you also may check the identity of the object by checking the Core Foundation "type id":

guard let identityPointer = certEntry["identity"],
CFGetTypeID(identityPointer) == SecIdentityGetTypeID() else {
// here you know that the cast will fail
}
let secIdentityRef = identityPointer as! SecIdentity // swiftlint:disable:this force_cast

Which YCbCr matrix to use? BT.709 or BT.601

Using takeUnretainedValue() will give you a CFTypeRef. This then needs to be downcast to a CFString. For example, your code could look like this:

if let colorAttachment = CVBufferGetAttachment(image, kCVImageBufferYCbCrMatrixKey, nil)?.takeUnretainedValue(),
CFGetTypeID(colorAttachment) == CFStringGetTypeID() {
let colorAttachmentString = colorAttachment as! CFString
print(colorAttachmentString)
print(colorAttachmentString == kCVImageBufferYCbCrMatrix_ITU_R_601_4)
print(colorAttachmentString == kCVImageBufferYCbCrMatrix_ITU_R_709_2)
}

Which prints:

ITU_R_601_4
true
false


Related Topics



Leave a reply



Submit