如何在 swift iOS 8.0 中解决这个问题,即 Type 'String!'不符合协议(protocol)“Equatable”
这是我的代码
func connection(connection: NSURLConnection, canAuthenticateAgainstProtectionSpace protectionSpace: NSURLProtectionSpace?) -> Bool
{
return protectionSpace?.authenticationMethod == NSURLAuthenticationMethodServerTrust
// Here I got this error near == sign
}
func connection(connection: NSURLConnection, didReceiveAuthenticationChallenge challenge: NSURLAuthenticationChallenge?)
{
if challenge?.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust
{
if challenge?.protectionSpace.host == "www.myhost.com"
// Here I got this error near == sign
{
let credentials = NSURLCredential(forTrust: challenge!.protectionSpace.serverTrust)
challenge!.sender.useCredential(credentials, forAuthenticationChallenge: challenge)
}
}
challenge?.sender.continueWithoutCredentialForAuthenticationChallenge(challenge)
}
最佳答案
我不同意关于简单升级到最新版本以获得解决方案的评论。 It's a better practice to unwrap optionals with a nil check在进行其他比较之前。我会按如下方式重写您的代码(尽管比需要的更冗长),这也应该可以解决您在所有版本中的问题:
func connection(connection: NSURLConnection, canAuthenticateAgainstProtectionSpace protectionSpace: NSURLProtectionSpace?) -> Bool
{
if let authenticationMethod = protectionSpace?.authenticationMethod
{
return authenticationMethod == NSURLAuthenticationMethodServerTrust
}
return false
}
func connection(connection: NSURLConnection, didReceiveAuthenticationChallenge challenge: NSURLAuthenticationChallenge?)
{
if let authenticationMethod = challenge?.protectionSpace.authenticationMethod
{
if authenticationMethod == NSURLAuthenticationMethodServerTrust
{
if challenge?.protectionSpace.host == "www.myhost.com"
{
let credentials = NSURLCredential(forTrust: challenge!.protectionSpace.serverTrust)
challenge!.sender.useCredential(credentials, forAuthenticationChallenge: challenge)
}
}
}
challenge?.sender.continueWithoutCredentialForAuthenticationChallenge(challenge)
}
关于ios - 类型 'String!' 不符合协议(protocol) 'Equatable',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26033774/