Possible Duplicate:
How to write to plist successfully?
I want to simply write the boolean state of a UISwitch(true or false) in a Plist file.
Here is what I already got, but the values do not change in the plist:
.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController {
IBOutlet UISwitch *mySwitch;
}
-(IBAction)changeThingsInPlist:(id)sender;
@end
.m
#import "ViewController.h"
BOOL changeThing;
#define PLIST_PATH @"/var/mobile/Library/Preferences/com.myname.plistname.plist"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
changeThing = [[[NSDictionary dictionaryWithContentsOfFile:PLIST_PATH]valueForKey:@"changeThing"]boolValue];
}
-(IBAction)changeThingsInPlist:(id)sender {
if (mySwitch.on = YES) {
changeThing = true;
}
else {
changeThing = false;
}
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end
I simply want to change the value(true or false) in the plist file by switching the uiswitch.
should I use NSUserDefaults? 🙂
I read into it, but I never got, how to set the plist path in the NSUserDefaults
Thank you
I would say that you should create an NSString and set the value of it based on the boolean value of the UISwitch.
Then you should put that string in the NSUserDefaults like so.
Then to retrieve this string you do this.
From there you can do whatever you want with that string.