如何在swift iOS 8.0即Type 'String!' does not conform to 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)
}发布于 2015-01-15 07:43:08
我不同意简单地升级到最新版本作为解决方案的评论。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)
}https://stackoverflow.com/questions/26033774
复制相似问题