2014-09-25 65 views
4

如何迅速的iOS 8.0即Type 'String!' does not conform to protocol 'Equatable'键入'String!'不符合协议“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) 
} 
+2

您是否正在运行旧的xcode6测试版? – 2014-09-25 09:05:48

+0

@MikePollard是的我是 – 2014-09-25 10:15:38

+5

升级到Xcode 6.0.1,一切都会好的。 – 2014-09-25 10:16:56

回答

0

我的意见不同意简单地升级到最新版本的解决方案。 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) 
}