简体   繁体   English

iOS版 - 钥匙扣 - 密码箱,不保存第一次启动阵列

[英]iOS - Keychain - Lockbox, not saving array on first launch

I am probably doing something wrong, but when I use the code below, and want Lockbox to save elements to the keychain on the first launch of the app nothing is stored. 我可能做错了什么,但是当我使用下面的代码,并希望Lockbox在应用程序的首次启动时将元素保存到钥匙串中时,没有存储任何内容。 If I then run the app again and add the elements in the else part, then it saves the data. 如果然后我再次运行该应用程序并将元素添加到else部分中,则它将保存数据。

Any suggestions? 有什么建议么?

NSString *value = @"";
NSArray *array = [Lockbox arrayForKey:@"TestKey"];
value = [array componentsJoinedByString:@"|"];

if(value == nil)
{
    BOOL result = NO;
    NSArray *narray = [NSArray arrayWithObjects:
                       [value stringByAppendingString:@"key1"],
                       [value stringByAppendingString:@"key2"],
                       [value stringByAppendingString:@"|http://www.url.com/"],
                       [value stringByAppendingString:@"|http://www.url2.com/"],nil];
    result = [Lockbox setArray:narray forKey:@"TestKey"];
}else{
    NSArray *narray = [NSArray arrayWithObjects:
                       [value stringByAppendingString:@"key1"],
                       [value stringByAppendingString:@"key2"],
                       [value stringByAppendingString:@"|http://www.url.com/"],
                       [value stringByAppendingString:@"|http://www.url2.com/"],nil];
    result = [Lockbox setArray:narray forKey:@"TestKey"];

    NSString *keyv1 =[array objectAtIndex:0];
    NSLog(@"key value %@",keyv1);

}

ha! 哈! got it .. didnt see it at first: 知道了..最初没有看到它:

the first time, nothin is stored in the keychain so array is NIL 第一次,nothin存储在钥匙串中,所以数组为NIL

value should then be [nil componentsJoinedBy...] 那么值应为[无componentsJoinedBy ...]

value is nil! 值是零!

so [nil stringByAppendinString will always be nil and an EMPTY array will be saved on first run 因此[nil stringByAppendinString将始终为nil,并且在第一次运行时将保存一个EMPTY数组


on second run array is NOT nil as Lockbox reads back the empty array (so then all works) 在第二个运行数组上,null不为零,因为Lockbox会读回空数组(因此所有工作都可以进行)

idea, change: 想法,改变:

NSArray *array = [Lockbox arrayForKey:@"TestKey"];
value = [array componentsJoinedByString:@"|"];

if(value==nil)
...

to

NSArray *array = [Lockbox arrayForKey:@"TestKey"];
if(array)
     value = [array componentsJoinedByString:@"|"];
else
     value = @"";

if(value.length)
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM