Swift Keychain SecItemUpdate返回errSecParam

Swift Keychain SecItemUpdate返回errSecParam,swift,keychain,Swift,Keychain,我可以将项目添加到钥匙链,甚至可以读出它们 然而,我正在努力使用SecItemUpdate更新一个值,并且似乎每次都返回errSecParam 为此,我创建了以下查询和属性(这里很可能是个问题) 然后用于更新 SecItemUpdate(查询为CFDictionary,属性为CFDictionary)应该在查询中指定类,而不是数据(用于更新),因此,例如,要在密钥链中添加/更新密码,应该如下所示 // adding item let addQuery: [CFString: Any] = [

我可以将项目添加到钥匙链,甚至可以读出它们

然而,我正在努力使用
SecItemUpdate
更新一个值,并且似乎每次都返回errSecParam

为此,我创建了以下查询和属性(这里很可能是个问题)

然后用于更新


SecItemUpdate(查询为CFDictionary,属性为CFDictionary)

应该在查询中指定类,而不是数据(用于更新),因此,例如,要在密钥链中添加/更新密码,应该如下所示

// adding item
let addQuery: [CFString: Any] = [
    kSecClass: kSecClassGenericPassword,
    kSecAttrService: service as CFString,        // eg. "www.mysite.com"
    kSecAttrAccount: name as CFString            // eg. "user"
    kSecValueData: password.data(using: .utf8)   // eg. "password"
] as CFDictionary

if errSecSuccess != SecItemAdd(addQuery, nil) { 
    // report error here
}

// updating item (same query is for SecItemDelete)

let updateQuery: [CFString: Any] = [
    kSecClass: kSecClassGenericPassword,
    kSecAttrService: service as CFString,        // eg. "www.mysite.com"
    kSecAttrAccount: name as CFString            // eg. "user"
] as CFDictionary

let newAttributes = [
    kSecAttrAccount: newName as CFString            // eg. "user1"
    kSecValueData: newPassword.data(using: .utf8)   // eg. "password1"
] as CFDictionary

if errSecSuccess != SecItemUpdate(updateQuery, newAttributes) {
   // report error here
}
// adding item
let addQuery: [CFString: Any] = [
    kSecClass: kSecClassGenericPassword,
    kSecAttrService: service as CFString,        // eg. "www.mysite.com"
    kSecAttrAccount: name as CFString            // eg. "user"
    kSecValueData: password.data(using: .utf8)   // eg. "password"
] as CFDictionary

if errSecSuccess != SecItemAdd(addQuery, nil) { 
    // report error here
}

// updating item (same query is for SecItemDelete)

let updateQuery: [CFString: Any] = [
    kSecClass: kSecClassGenericPassword,
    kSecAttrService: service as CFString,        // eg. "www.mysite.com"
    kSecAttrAccount: name as CFString            // eg. "user"
] as CFDictionary

let newAttributes = [
    kSecAttrAccount: newName as CFString            // eg. "user1"
    kSecValueData: newPassword.data(using: .utf8)   // eg. "password1"
] as CFDictionary

if errSecSuccess != SecItemUpdate(updateQuery, newAttributes) {
   // report error here
}