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
UISwitch(IntPtr) A constructor used when creating managed representations of unmanaged objects; Called by the runtime. UISwitch(NSCoder) A constructor that initializes the object from the data stored in the unarchiver object.
A control that offers a binary choice, such as on/off.
To save it
[newContact setObject:[NSNumber numberWithBool:important.on] forKey:@"important"];
To retrieve it
BOOL on = [[newContact objectForKey:@"important"] boolValue];
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With