i've been looking at a lot of posts with a similar problem here on Stackoverflow, but i still cannot figure out why i can't write/read my plist. Here is my method to load my plist into the app. This should copy the plist to the device's documents folder and not resources folder, correct?
-(void)loadPlist:(NSString*)plistString {
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist", plistString]];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:path]) {
NSString *bundle = [[NSBundle mainBundle]pathForResource:plistString ofType:@"plist"];
[fileManager copyItemAtPath:bundle toPath:path error:&error];
} else {
NSLog(@"%@ already exists",path);
}
}
This is how i write to my plist:
-(void)writeScore:(NSString*)objectstring forPlayer:(int)playerTag {
switch (playerTag) {
case 1:
NSLog(@"Wrote to 1");
holeInt = [P1hole_Label.text integerValue];
plistpath = [[NSBundle mainBundle]pathForResource:@"Scores" ofType:@"plist"];
plistdata = [[NSMutableDictionary alloc]initWithContentsOfFile:plistpath];
[plistdata setObject:[NSString stringWithString:objectstring] forKey:[NSString stringWithFormat:@"Hole %i", holeInt]];
[plistdata writeToFile:plistpath atomically:YES];
break;
Etc.
And here is how i read the plist:
-(void)createNewHoleLabel:(NSString*)plistString andSetX:(int)x {
//SetX; 0 = P1, 1 = P2 etc.
UILabel *label = [UILabel new];
[label setFrame:CGRectMake(80 + 53 * x, 93 + 26 * integer, 50, 25)];
NSString *path = [[NSBundle mainBundle]pathForResource:plistString ofType:@"plist"];
NSMutableDictionary *dict = [[NSMutableDictionary alloc]initWithContentsOfFile:path];
label.text = [dict objectForKey:[NSString stringWithFormat:@"Hole %i", integer + 1]];
[label setAdjustsFontSizeToFitWidth:YES];
[label setTextAlignment:NSTextAlignmentCenter];
integer++;
[scrollview addSubview:label];
}
I hope you can help me. Please ask for further details if needed.
In writeScore:forPlayer:
plistpath = [[NSBundle mainBundle]pathForResource:@"Scores" ofType:@"plist"];
...
[plistdata writeToFile:plistpath atomically:YES];
You are trying to write to the the App's bundle which can not be done on the device. You should be writing the the Documents directory.