更改plist文件中的值

时间:2015-03-29 17:52:43

标签: ios objective-c plist

我创建了plist文件,通过更改background-color文件中的WallpaperViewController,将CalculatorViewControllervalue更改为plist。 我的代码在 iOS 模拟器中工作得很完美,但在设备上它不起作用。 这是我的代码:

WallpaperViewController

- (IBAction)changeBack:(UIButton *)sender {



NSString *buttonname = sender.currentTitle;

NSString *hello = [NSString stringWithFormat:NSLocalizedString(@"The calculator background is %@",nil) ,buttonname];

NSString *path = [[NSBundle mainBundle] pathForResource:@"CB" ofType:@"plist"];

NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:path];



if ([buttonname isEqualToString:@"iPhone6Calwall"]) {

    [dict setObject:buttonname forKey:@"Calculatorbackground"];

    [dict writeToFile:path atomically:YES];



} else if ([buttonname isEqualToString:@"Green"]) {

        [dict setObject:buttonname forKey:@"Calculatorbackground"];

        [dict writeToFile:path atomically:YES];



} else if ([buttonname isEqualToString:NSLocalizedString(@"Yellow",nil)]) {

    [dict setObject:buttonname forKey:@"Calculatorbackground"];

    [dict writeToFile:path atomically:YES];



} else if ([buttonname isEqualToString:@"Brown"]) {

        [dict setObject:buttonname forKey:@"Calculatorbackground"];

        [dict writeToFile:path atomically:YES];



} else if ([buttonname isEqualToString:@"Purple"]) {

    [dict setObject:buttonname forKey:@"Calculatorbackground"];

    [dict writeToFile:path atomically:YES];



} else if ([buttonname isEqualToString:@"Red"]) {

    [dict setObject:buttonname forKey:@"Calculatorbackground"];

    [dict writeToFile:path atomically:YES];

CalculatorViewController

// Read plist from bundle and get Root Dictionary out of it

NSString *path = [[NSBundle mainBundle] pathForResource:@"CB" ofType:@"plist"];

NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];

NSString *check = [dict objectForKey:@"Calculatorbackground"];





if ([check isEqual:@"iPhone6Calwall"]) {

    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"iPhone6Calwall"]]];

} else if ([check isEqualToString:@"Red"]) {

    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"BMIbackground"]]];



} else if ([check isEqualToString:@"Green"]) {

    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"Tipbackground"]]];



} else if ([check isEqualToString:@"Yellow"]) {

    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"CalYellow"]]];



} else if ([check isEqualToString:@"Brown"]) {

    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"CalBrown"]]];



} else if ([check isEqualToString:@"Purple"]) {

    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"CalPurple"]]];



}

2 个答案:

答案 0 :(得分:1)

错误在于您无法写入应用程序包,请使用文档目录。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths firstObject];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName];

NSError *error;
BOOL status = [data writeToFile:filePath options:NSDataWritingAtomic error:&error];
if (status == NO) {
    NSLog(@"error: %@", error)
}

错误参数对您有利,请使用它们。而不是writeToFile:atomically:使用writeToFile:options:error:,检查错误返回,然后如果有错误,请检查error参数以查看错误是什么。

答案 1 :(得分:0)

上周有同样的问题。您正试图写入您无权限的NSBundle mainBundle。请改用Documents文件夹中的路径。