xcode - 类型 'CFStringRef' 不符合 Xcode 6.1 中的协议(protocol) 'Hashable'

标签 xcode swift

在我的应用程序中,我有一个在 Xcode 6 中工作的钥匙串(keychain)访问类,但现在在 Xcode 6.1 中我遇到了一些错误,这是第一个错误:类型 'CFStringRef' 不符合协议(protocol) 'Hashable':

private class func updateData(value: NSData, forKey keyName: String) -> Bool {
    let keychainQueryDictionary: NSMutableDictionary = self.setupKeychainQueryDictionaryForKey(keyName)

    let updateDictionary = [kSecValueData:value] //HERE IS THE ERROR

    // Update
    let status: OSStatus = SecItemUpdate(keychainQueryDictionary, updateDictionary)

    if status == errSecSuccess {
        return true
    } else {
        return false
    }
}

我也收到与第一个错误类似的错误,但它是:Type 'CFStringRef' does not conform to protocol 'NSCopying' 这是我收到此错误的部分:

private class func setupKeychainQueryDictionaryForKey(keyName: String) -> NSMutableDictionary {
    // Setup dictionary to access keychain and specify we are using a generic password (rather than a certificate, internet password, etc)

    var keychainQueryDictionary: NSMutableDictionary = [kSecClass:kSecClassGenericPassword] 

    // HERE IS THE ERROR ↑↑↑

    // Uniquely identify this keychain accessor
    keychainQueryDictionary[kSecAttrService as String] = KeychainWrapper.serviceName

    // Uniquely identify the account who will be accessing the keychain
    var encodedIdentifier: NSData? = keyName.dataUsingEncoding(NSUTF8StringEncoding)

    keychainQueryDictionary[kSecAttrGeneric as String] = encodedIdentifier
    keychainQueryDictionary[kSecAttrAccount as String] = encodedIdentifier

    return keychainQueryDictionary
}

有人能告诉我如何解决这些错误吗?

最佳答案

CFStringRefNSString 桥接,后者与 String 桥接。最简单的解决方案是将 kSecValueDatakSecClass 转换为 StringNSString:

这里:

let updateDictionary = [kSecValueData as String: value]

在这里:

var keychainQueryDictionary: NSMutableDictionary = [kSecClass as NSString: kSecClassGenericPassword]

关于xcode - 类型 'CFStringRef' 不符合 Xcode 6.1 中的协议(protocol) 'Hashable',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26569000/

相关文章:

ios - react native 错误 - react-native-xcode.sh : line 45: react-native: command not found Command/bin/sh failed with exit code 127

swift - 刷新时出现 fatal error : Array index out of range. Swift

ios - 如果我已经对 UIViewController 进行子类化,如何对 SwipeTableViewController 进行子类化?另外,如何向用户显示键盘? ( swift )

ios - 在代码中更改纵横比值

ios - Xcode 5 使用不同的图像取决于是否使用 iOS 7 或更低版本

ios - 使用 Xcode5.1 为 iOS 编译 FFmpeg 错误

ios - dyld:未加载库:libperl.dylib 引用自:perl5.18

iphone - NSString 解析

swift - 循环中的异步 DispatchQueue

swift - println 错误...EXC_BREAKPOINT(代码=EXC_I386_BPT,子代码=0x0)swift