Read/Write on Plist file without using the iPhone SImulator?
i am using an example from the iphone develope开发者_如何学JAVAr book from apress. the problem is that this example only works on the simulator im trying to figure out how i can make it work on the device. This chapter isn't working at all. below is the sample code. data.plist is located in the resource folder.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:kFilename];
Below then checks to see if the file is located. this is skipped, so im guessing this does not find the file.
if ([[NSFileManager defaultManager]fileExistsAtPath:filePath]) {
NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
BGDataSave = [array objectAtIndex:0];
NSLog(@"%@", BGDataSave);
price.text = [array objectAtIndex:1];
percent.text = [array objectAtIndex:2];
salepriceLabel.text = [array objectAtIndex:3];
origpriceLabel.text = [array objectAtIndex:4];
}
If data.plist is in the resources directory, get the path like this:
filePath = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"];
If the intent is to modify the file, then you would copy it to the documents directory when it does not exist. You cannot write to files in the bundle, which is where resources are stored.
Did you write the file into the documents directory in the first place? If not, it won't be there....
精彩评论