当前位置 : 主页 > 手机开发 > ios >

ios – secItemAdd在swift中保持返回-50错误

来源:互联网 收集:自由互联 发布时间:2021-06-11
尝试在安全链中添加项目时,请继续获取-50. var query = [String:AnyObject]() query[kSecClass as String] = kSecClassGenericPassword query[kSecAttrAccount as String] = "a" query[kSecValueData as String] = "b" let result = SecItem
尝试在安全链中添加项目时,请继续获取-50.

var query = [String:AnyObject]()
    query[kSecClass as String] = kSecClassGenericPassword
    query[kSecAttrAccount as String] = "a"
    query[kSecValueData as String] = "b"
    let result = SecItemAdd(query as CFDictionary, nil);

结果是-50.无法弄清楚为什么,需要帮助..在此先感谢.

我相信kSecValueData键的值需要是NSData,而不是String或NSString.尝试将字符串编码为数据(例如使用UTF-8编码).未经测试的片段:

query[kSecValueData as String] = "b".dataUsingEncoding(NSUTF8StringEncoding)

为了将来参考,错误代码-50对应于errSecParam,SecBase.h头文件的含义为:“传递给函数的一个或多个参数无效.”如果再次看到此错误,请尝试使用查询字典更改传入的值.

网友评论