本文介绍了SecIdentity + Force Cast Violation:应该避免强制转换.(force_cast)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请不要标记重复的问题.

Please dont mark repetitive question.

大家好,

我正在做 NSURLAuthenticationMethodClientCertificate,其中我使用了以下代码.如果我不使用 swiftlint,在哪个代码中很好.但是当我使用 swiftlint 时,我收到了这个错误并且无法解决这个问题.尝试了很多请帮助我.在下面发布代码.

I am doing NSURLAuthenticationMethodClientCertificate in which I am using the following code. In which code is fine if I dont use swiftlint. But when I use swiftlint I am getting this error and unable to solve this. Tried a lot please help me . Posting the code below.

    var items: CFArray?
    //let PKCS12Data = DataSourceManager.sharedInstance.serverPolicyManager?.PKCS12Data
    securityError = SecPKCS12Import(certData, options, &items)

    if securityError == errSecSuccess {
        let certItems: CFArray = items as CFArray!
        let certItemsArray: Array = certItems as Array
        let dict: AnyObject? = certItemsArray.first
        if let certEntry: Dictionary = dict as? Dictionary<String, AnyObject> {

            // grab the identity
            let identityPointer = certEntry["identity"]
            let secIdentityRef = identityPointer as!  SecIdentity
            print("\(String(describing: identityPointer))  :::: \(secIdentityRef)")
            // grab the trust
            let trustPointer: AnyObject? = certEntry["trust"]
            let trustRef: SecTrust? = trustPointer as! SecTrust
            print("\(String(describing: trustPointer))  :::: \(trustRef)")
            // grab the cert
            let chainPointer: AnyObject? = certEntry["chain"]
            identityAndTrust = IdentityAndTrust(identityRef: secIdentityRef, trust: trustRef!, certArray:  chainPointer!)

        }

    }

我在以下几行中收到强制施放违规.

I am getting forcecast violation in the below lines.

让 secIdentityRef = identityPointer as!安全身份

let trustRef: SecTrust?= 信任指针为!SecTrust

推荐答案

基本上,你所做的是强制向下转型,这意味着你保证你的 identityPointertrustPointer 分别是 SecIdentitySecTrust 类的对象.但如果他们不是呢?您将它们作为 AnyObject 从字典中取出,因此通常它们可能不会转换为目标类.Swiftlint 告诉您,强制转换是一种不好的做法,并希望您避免使用它.

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.

但是,对于 CoreFoundation 类型,您似乎不能使用条件转换 as?,因此强制转换是您唯一的选择.对于这种特殊情况,您可以在添加特殊注释的代码中禁用 Swiftlint 规则.

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

为了安全起见,您还可以通过检查 Core Foundation 的type id"来检查对象的身份:

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

这篇关于SecIdentity + Force Cast Violation:应该避免强制转换.(force_cast)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 22:06