2016-03-01 99 views
2

您是否可以在一个forUserAccount下存储多个键/值对?使用锁匠在钥匙串中存储多个值

try? Locksmith.saveData(["access_token" : access_token], forUserAccount: "UserData") 
try? Locksmith.saveData(["email" : email!], forUserAccount: "UserData") 
try? Locksmith.saveData(["markets" : market], forUserAccount: "UserData") 

此代码在寻找"email."时失败。我能做到这一点的唯一方法是创建多个forUserAccount字符串:UserData,UserData1,UserData3等。我试过updateData没有运气。如果forUserAccount必须是唯一的,它是干什么用的?如果是这样的话,似乎没有必要。

回答

2
let userIDNumber = userID as NSNumber 
let userIDString : String = userIDNumber.stringValue 
let keychainData = [_keychainEmailKey: email, _keychainPasswordKey: password, _keychainUserIDKey: userIDString] 

Locksmith.updateData(keychainData, forUserAccount: email) 

其中_keychainEmailKey等是键的常数。所以forUserAccount应该是唯一的,可能是覆盖以前的值。

1

就像@ nyekimov的回答所暗示的那样,可以为一个帐户存储值的字典。为了澄清,这里的代码片段:

do 
    { 
     try Locksmith.saveData(["user": "notMyRealUsername", "password":"123456"], forUserAccount: "myUserAccount") //store it 
     let dictionary = Locksmith.loadDataForUserAccount("myUserAccount") //load it 
     print(dictionary) //let's see what we've got 
    } 
    catch 
    { 
    // handle errors here  
    } 

使用时存在的帐户try Locksmith.updateData(["user": "notMyRealUsernameEither", "password":"123456"], forUserAccount: "myUserAccount")