当我使用Xcode版本6时,我用于访问Keychain的类正在运行,但现在在版本6.1中它无法正常工作
以下是Keychain访问类的一部分:
class func setData(value: NSData, forKey keyName: String) -> Bool {
var keychainQueryDictionary: NSMutableDictionary = self.setupKeychainQueryDictionaryForKey(keyName)
keychainQueryDictionary[kSecValueData as String] = value
// Protect the keychain entry so it's only valid when the device is unlocked
keychainQueryDictionary[kSecAttrAccessible as String] = kSecAttrAccessibleWhenUnlocked
let status: OSStatus = SecItemAdd(keychainQueryDictionary, nil)
if Int(status) == errSecSuccess { //I GET THE ERROR HERE
return true
} else if Int(status) == errSecDuplicateItem {
return self.updateData(value, forKey: keyName)
} else {
return false
}
}
这不是它在这里做的唯一地方是代码的另一部分:
class func removeObjectForKey(keyName: String) -> Bool {
let keychainQueryDictionary: NSMutableDictionary = self.setupKeychainQueryDictionaryForKey(keyName)
//Delete
let status: OSStatus = SecItemDelete(keychainQueryDictionary);
if Int(status) == errSecSuccess { //GET ERROR HERE
return true
} else {
return false
}
}
看起来像问题是errSecSuccess可以有人帮助我
答案 0 :(得分:3)
OSStatus
是Int32
的别名,因此我认为您可以通过删除转化为Int
来解决此问题,例如:
if status == errSecSuccess {
附注:您的多个if/elseif/else
可以替换为switch
:
switch (status) {
case errSecSuccess:
...
case errSecDuplicateItem:
...
default:
...
}
在我看来,更具可读性