类型'CFStringRef'不符合Xcode 6.1中的协议'Hashable'

时间:2014-10-26 01:42:03

标签: xcode swift

在我的应用程序中,我有一个在Xcode 6中工作的Keychain访问类,但现在在Xcode 6.1中我得到一些错误,这是第一个:Type' CFStringRef'不符合协议' 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'不符合协议' 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
}

有人可以告诉我如何解决这些错误。

2 个答案:

答案 0 :(得分:8)

CFStringRefNSString桥接,与String桥接。最简单的解决方案是将kSecValueDatakSecClass投射到StringNSString s:

下面:

let updateDictionary = [kSecValueData as String: value]

在这里:

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

答案 1 :(得分:4)

我认为它会更具可读性

let keychainQueryDictionary : [String: AnyObject] = [
  kSecClass       : kSecClassGenericPassword,
  kSecAttrService : serviceIdentifier,
  kSecAttrAccount : accountName
]