As you can see from the title I basically want to update a plist. Actually the app user will type his data in data fields and I will take them and save them in a plist. He can add as many objects as he likes.
I have tried a huge number of different codes for that staff and none of them works. First of all take a look at my plist (I am presenting you the xml code so you can understand it)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<dict>
<key>name</key>
<string>TEST</string>
<key>code</key>
<string>TEST</string>
<key>longitude</key>
<integer>22</integer>
<key>latitude</key>
<integer>10</integer>
</dict>
</array>
</plist>
That was just a simple entry. Now here is the lates code I have used but I had no luck at all
NSMutableDictionary *newBuilding = [NSMutableDictionary alloc];
[newBuilding setValue:@"Test1" forKey:@"name"];
[newBuilding setValue:@"Test2" forKey:@"code"];
[newBuilding setValue:@"10" forKey:@"latitude"];
[newBuilding setValue:@"20" forKey:@"longitude"];
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"BuildingsInfo.plist"];
NSMutableArray *newArray = [NSMutableArray arrayWithContentsOfFile:plistPath];
if (nil == newArray) {
newArray = [[NSMutableArray alloc] initWithCapacity:0];
}
[newArray addObject:newBuilding];
[newArray writeToFile:plistPath atomically: YES];
}
Can anybody spot any mistake? I have written it a lot of times, I have checked every token but I have found no mistakes. I cannot understand why I does not want to work!
Thanks a lot guys
For what it’s worth, you never initialized
newBuilding.