2014-09-25 62 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) 
}