无法调用SecPolicyCreateSSL'使用类型'(Bool,String?)'的参数列表

时间:2015-08-10 17:20:22

标签: swift2

HTTPSecurity.swift:124:22: Cannot invoke 'SecPolicyCreateSSL' with an argument list of type '(Bool, String?)'

我在尝试构建包含此代码的项目时遇到上述错误:

public func isValid(trust: SecTrustRef, domain: String?) -> Bool {

        var tries = 0
        while(!self.isReady) {
            usleep(1000)
            tries += 1
            if tries > 5 {
                return false //doesn't appear it is going to ever be ready...
            }
        }
        var policy: SecPolicyRef
        if self.validatedDN {
            policy = SecPolicyCreateSSL(true, domain)
        } else {
            policy = SecPolicyCreateBasicX509()
        }
        SecTrustSetPolicies(trust,policy)
        if self.usePublicKeys {
            if let keys = self.pubKeys {
                var trustedCount = 0
                let serverPubKeys = publicKeyChainForTrust(trust)
                for serverKey in serverPubKeys as [AnyObject] {
                    for key in keys as [AnyObject] {
                        if serverKey.isEqual(key) {
                            trustedCount++
                            break
                        }
                    }
                }
                if trustedCount == serverPubKeys.count {
                    return true
                }
            }
        } else if let certs = self.certificates {
            let serverCerts = certificateChainForTrust(trust)
            var collect = Array<SecCertificate>()
            for cert in certs {
                if let c = SecCertificateCreateWithData(nil,cert) {
                    collect.append(c)
                }
           }
            SecTrustSetAnchorCertificates(trust,collect)
            var result: SecTrustResultType = 0
            SecTrustEvaluate(trust,&result)
            let r = Int(result)
            if r == kSecTrustResultUnspecified || r == kSecTrustResultProceed {
                var trustedCount = 0
                for serverCert in serverCerts {
                    for cert in certs {
                        if cert == serverCert {
                            trustedCount++
                            break
                        }
                    }
                }
                if trustedCount == serverCerts.count {
                    return true
                }
            }
        }
        return false
    }

此代码来自:

https://github.com/mpclarkson/SwiftHTTP/blob/swift-2/HTTPSecurity.swift#L124

1 个答案:

答案 0 :(得分:0)

使用旧时尚布尔方法,实际上是UInt8。方法也使用CFString而非String。 简单的解决方案如下:

policy = SecPolicyCreateSSL(1, domain as CFString)

更复杂的解决方案是Bool的使用扩展名:

extension Bool {

    var booleanValue : Boolean {
        return self ? 1 : 0
    }

    init(booleanValue : Boolean) {
        self = booleanValue == 0 ? false : true
    }
}

然后你可以使用:

policy = SecPolicyCreateSSL(true.booleanValue, domain as CFString)