This is a follow up question on my first queries regarding retrieving data on plist. Right now i have manage to detect users touches made on my view with random image call o
Try this:
-(void)add:(NSRunningApplication *) app {
if ([self contains:app]) return;
[self.apps addObject:app.localizedName];
[self.apps writeToFile:self.dataFile atomically:YES];
}
Here are two methods to read and write values from a plist using an NSDictionary:
- (NSMutableDictionary*)dictionaryFromPlist {
NSString *filePath = @"myPlist.plist";
NSMutableDictionary* propertyListValues = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
return [propertyListValues autorelease];
}
- (BOOL)writeDictionaryToPlist:(NSDictionary*)plistDict{
NSString *filePath = @"myPlist.plist";
BOOL result = [plistDict writeToFile:filePath atomically:YES];
return result;
}
and then in your code block somewhere:
// Read key from plist dictionary
NSDictionary *dict = [self dictionaryFromPlist];
NSString *valueToPrint = [dict objectForKey:@"Executable file"];
NSLog(@"valueToPrint: %@", valueToPrint);
// Write key to plist dictionary
NSString *key = @"Icon File";
NSString *value = @"appIcon.png";
[dict setValue:value forKey:key];
// Write new plist to file using dictionary
[self writeDictionaryToPlist:dict];
Look into the documentation for -initWithContentsOfFile:
You should be able to create dictionaries and arrays from plists, provided that they are standard.
In this case, you can call [[NSDictionary alloc] initWithContentsOfFile:@"path/to/plist"];
And then you can use -valueForKey:
to get the data you need.
The plist part is well explained in other answers.
But if you are storing/reading with CGPoint
I would strongly suggest you to use the following very handy methods, CGPointFromString
and NSStringFromCGPoint
see Apple documentation for more information.
Note there are also other utilities to work with graphics structures like CGPointEqualToPoint
, CGRectContainsPoint
, CGRectIntersectsRect
or CGRectContainsRect
... see Apple documentation for more information