2

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];
bhumi
  • 57
  • 1
  • 8

2 Answers2

6

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];
rckoenes
  • 69,092
  • 8
  • 134
  • 166
0

Also you can use Shorthand like

[keychain setObject:@(YES) forKey:(__bridge BOOL)kSecAttrIsInvisible];
Retro
  • 3,985
  • 2
  • 17
  • 41
  • 1
    Shorthand are called Literals and are not a feature of Objective-C 2.0 but [Clang 3.7](http://clang.llvm.org/docs/ObjectiveCLiterals.html) and used in LLVM 4.0 – rckoenes Mar 31 '15 at 13:26