ios - 类型 'String!' 不符合协议(protocol) 'Equatable'

标签 ios swift

如何在 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/

相关文章:

ios - AVAudioPlayer 音量范围?

iphone - iOS - 推送通知 : does it always display a notification

ios - 调整 UITableViewCell 的高度

arrays - 与 Swift 中的另一个数组相比对数组重新排序

swift - SCNText 对齐在 iOS 中不起作用

ios - AVFoundation 以 MP4 格式录制视频

ios - 在 UITableView 中滑动取消而不是删除

ios - 使用单击的按钮通过 tableView 传递数据

swift - UI 测试包标识符和代码覆盖率

objective-c - iOS 中的 ViewDidLoad 和 UINavigationController?