本文介绍了将阵列保存在钥匙串中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将数组保存在钥匙串中,但是无法将数组转换为NSData.我已经准备好用于保存字符串的函数,但是我不知道如何从数组中获取valueData
.
I am trying to save an array in keychain but I can't convert an array to NSData. I have my function prepared for saving strings but I don't know how to get a valueData
from an array.
func add(key: String, value: AnyObject) {
let service = NSBundle.mainBundle().bundleIdentifier!
let valueData: NSData! = value.dataUsingEncoding(NSUTF8StringEncoding,
allowLossyConversion: false)
let secItem = [
kSecClass as String : kSecClassGenericPassword as String,
kSecAttrService as String : service,
kSecAttrAccount as String : key,
kSecValueData as String : valueData
]
let result: UnsafeMutablePointer<AnyObject?> = nil
let status = Int(SecItemAdd(secItem, result))
if status == Int(errSecDuplicateItem){
self.update(key, newData: value)
} else {
print("An error occurred with code \(status)")
}
}
推荐答案
您可以在保存时在数组上使用NSKeyedArchiver.archivedDataWithRootObject
,在加载时可以在NSKeyedUnarchiver.unarchiveObjectWithData
上使用.
You can use NSKeyedArchiver.archivedDataWithRootObject
on your array when saving, and NSKeyedUnarchiver.unarchiveObjectWithData
when loading.
这篇关于将阵列保存在钥匙串中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!