22

I am a newbie iOS programmer and I have a problem.

I currently work on iOS Core Data and my problem is that I want to insert data into a boolean attribute to a database by taking the value of a UISwitch.

The problem is that i don't know what it the method i have to call (e.g .text does the same thing but for UITextField). I have done a small google search but no results. Here is some code:

[newContact setValue:howMany.text forKey:@"quantity"]; 
[newContact setValue:important.??? forKey:@"important"]; 

howmany is a textfield, important is a UISwitch

Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
Eristikos
  • 485
  • 4
  • 9
  • 24

5 Answers5

22

To save it

[newContact setObject:[NSNumber numberWithBool:important.on] forKey:@"important"]; 

To retrieve it

BOOL on = [[newContact objectForKey:@"important"] boolValue];
Paul.s
  • 38,494
  • 5
  • 70
  • 88
Joel Kravets
  • 2,473
  • 19
  • 16
17

Have you looked at the docs for UISwitch? Generally ou should make the docs your first point of call when searching for information, then turn to google and then to stack overflow if you really can't find what your after.

You want the @property(nonatomic, getter=isOn) BOOL on property like:

important.isOn

If you haven't got Core Data set to use primitives you may have to wrap that boolean in an NSNumber:

[NSNumber numberWithBool:important.isOn]
Paul.s
  • 38,494
  • 5
  • 70
  • 88
  • 3
    That should be important.on or [important isOn]. important.isOn won't work. – Nick Lockwood Feb 09 '12 at 21:25
  • @NickLockwood The doc's declare the property as `@property(nonatomic, getter=isOn) BOOL on` – Paul.s Feb 09 '12 at 21:27
  • 3
    @Paul.s right, but `isOn` is only used when you actually use it as a method. If you're using dot syntax, you use the name of the property: `on`. – Dave DeLong Feb 09 '12 at 21:31
  • Yes so the property is "on" and the getter method is "isOn". To access it using property dot syntax, use "on". To access it using method syntax use "isOn" - try it and get back to me ;-) – Nick Lockwood Feb 09 '12 at 21:32
  • 6
    @NickLockwood I've just tried `NSLog(@"%d %d %d ", thing.isOn, [thing isOn], thing.on);` and all are equivalent. – Paul.s Feb 09 '12 at 21:40
  • Huh, that's weird. Maybe it's just [important on] that doesn't work then. My mistake. (+1 for you) – Nick Lockwood Feb 09 '12 at 21:42
  • @DaveDeLong @NickLockwood I'm confused because I thought `thing.on` would be turned into `[thing on]` behind the scenes. `thing.on` works but `[thing on]` throws an `NSInvalidArgumentException`??? – Paul.s Feb 09 '12 at 21:45
  • Yes, that's as expected because the getter method is defined as isOn, so there is no "on" method. It's kinda weird that .isOn gets changed to [... isOn] - I don't know if that's a bug or a feature, but I guess you can call other methods that aren't defined as properties using dot syntax, so there's no reason why it wouldn't work. (I see people using array.count all the time for example). – Nick Lockwood Feb 09 '12 at 21:47
5

The other posters are correct that you need to use the isOn method to get the value, however this returns a BOOL value, which you can't pass directly to setValue:forKey because that method expects an object.

To set the value on your core data object, first wrap it in an NSNumber, like this:

NSNumber *value = [NSNumber numberWithBool:important.on];
[newContact setValue:value forKey:@"important"];
Nick Lockwood
  • 40,865
  • 11
  • 112
  • 103
  • Putting NSNumber data into a boolean attribute is correct? I have corrected my code based on your answer but the error still remains after saving data. – Eristikos Feb 09 '12 at 22:02
  • NSNumber is the official way to wrap a BOOL as an object, so it should work. What error are you seeing? – Nick Lockwood Feb 09 '12 at 22:18
  • Another option is to declare the important property as a BOOL on your core data class using @property(monatomic, assign) BOOL important; and then use "@dynamic important;" in the .m file to synthesize it. You can then just set it directly as a BOOL using newContact.important = important.on; – Nick Lockwood Feb 09 '12 at 22:21
4

I used

[NSString stringWithFormat:@"%d",(self.allNotificationSwitch.isOn ? 0:1)];

And

[NSString stringWithFormat:@"%@",(self.allNotificationSwitch.isOn ? @"Yes":@"No")];
Avijit Nagare
  • 8,482
  • 7
  • 39
  • 68
0
[newContact setBool:[NSNumber numberWithBool:important.on] forKey:@"important"];
NeverBe
  • 5,213
  • 2
  • 25
  • 39