Luke
Luke

Reputation: 81

Reading BOOL from plist, writing over it with new value

I've established how to read a BOOL from a plist, but I don't know how to write over the BOOL value I originally read.

The BOOL I'm reading is buried within my plist. When the view loads I take a dictionary from the plist that contains all the info for the view (detail view at the end of a drill down), and the BOOL is inside that dictionary. I can change the value of the BOOL in the dictionary once it's been brought into the app, but I don't know how to write this back to the postion within the plist.

Hopefully this is making sense?

Cheers!

Upvotes: 4

Views: 6877

Answers (3)

Fabio Poloni
Fabio Poloni

Reputation: 8371

Write a NSDictionary to a Property List:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"FileName.plist"];

NSDictionary *myDict = [[NSDictionary alloc] init];
[myDict setObject:[NSNumber numberWithBool:YES] forKey:@"myKey"];
[myDict writeToFile:filePath atomically:YES];

Read the Boolean:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"FileName.plist"];

NSDictionary *myDict = [[NSDictionary alloc] initWithContentsOfFile:filePath];
BOOL myBool = [[myDict objectForKey:@"myKey"] boolValue];

You can also put the filePath code into this function:

- (NSString *)getFilePath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"FileName.plist"];
return filePath;
}

And use it like this: (for reading)

NSDictionary *myDict = [[NSDictionary alloc] initWithContentsOfFile:[self getFilePath]];
BOOL myBool = [[myDict objectForKey@"myKey"] boolValue];

And this: (for writing)

NSDictionary *myDict = [[NSDictionary alloc] init];
[myDict setObject:[NSNumber numberWithBool:YES] forKey:@"myKey"];
[myDict writeToFile:[self getFilePath] atomically:YES];

Upvotes: 11

Rui
Rui

Reputation: 516

Try this function:

 -(void) setStringToPropertyList:(NSString*) pList withKey:(NSString*)key andValue:(NSString*)val{
//the path.
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *plistPath = [rootPath stringByAppendingPathComponent:pList];
//read the dictionary.
NSMutableDictionary *temp = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
//modify the desired key of the dictionary.
[temp setValue:val forKey:key];
//rewrite the diccionary again.
if([temp writeToFile: plistPath atomically:YES]){
   NSLog(@"setStringToPropertyList: pList updated");
}else{
   NSLog(@"setStringToPropertyList: error writing the file");
}
//memory management
if(!temp) [temp release];
}

This function allows you to write properties to a pList, parameteres:

pList = name of the file

key = key of the property

val = value of the property

Hope it helps.

Upvotes: 0

Jhaliya - Praveen Sharma
Jhaliya - Praveen Sharma

Reputation: 31722

For writing BOOL to .plist use below code

NSMutableDictionary* myDict = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];
[myDict setObject:[NSNumber numberWithBool:YES] forKey:@"LSUIElement"];
[myDict writeToFile:plistPath atomically:NO];

Also have look at the post

Upvotes: 2

Related Questions