how to save bool value in KeychainItemWrapper

2019-07-21 03:57发布

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];

2条回答
太酷不给撩
2楼-- · 2019-07-21 04:37

Also you can use Shorthand like

[keychain setObject:@(YES) forKey:(__bridge BOOL)kSecAttrIsInvisible];
查看更多
老娘就宠你
3楼-- · 2019-07-21 04:46

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];
查看更多
登录 后发表回答