how to save bool value in KeychainItemWrapper

懵懂的女人 提交于 2019-12-11 02:16:20

问题


I want to store bool value in KeychainItemWrapper, how to store ?

I have tried this code, but it gives me error.

 [keychain setObject:YES forKey:(__bridge BOOL)kSecAttrIsInvisible];

回答1:


The BOOL is a primitive type and the setObject:forKey: only excepts classes that derive from NSObject.

So use NSNumber it has a special method for it + numberWithBool::

[keychain setObject:[NSNumber numberWithBool:YES] forKey:@"someKey"];

And if you need to bool again:

 NSNumber *value = [keychain objectForKey:@"someKey"];
 BOOL boolValue = [value boolValue];



回答2:


Also you can use Shorthand like

[keychain setObject:@(YES) forKey:(__bridge BOOL)kSecAttrIsInvisible];


来源:https://stackoverflow.com/questions/29369498/how-to-save-bool-value-in-keychainitemwrapper

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!