首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >键入'String!‘不符合协议“Equatable”

键入'String!‘不符合协议“Equatable”
EN

Stack Overflow用户
提问于 2014-09-25 16:20:51
回答 1查看 1.2K关注 0票数 4

如何在swift iOS 8.0即Type 'String!' does not conform to protocol 'Equatable'中解决此问题

以下是我的代码

代码语言:javascript
复制
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)
}
EN

回答 1

Stack Overflow用户

发布于 2015-01-15 07:43:08

我不同意简单地升级到最新版本作为解决方案的评论。It's a better practice to unwrap optionals with a nil check,然后再进行其他比较。我将重写您的代码(尽管比需要的代码更详细),如下所示,这也可以在所有版本中修复您的问题:

代码语言:javascript
复制
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)
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26033774

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档