【问题标题】:Mac os x writeToFile not working using xcode5Mac os x writeToFile 无法使用 xcode5
【发布时间】:2013-10-30 06:42:58
【问题描述】:

我正在尝试修改 plist 并保存更改,但它不起作用:

这是我的代码:

        NSString *myPath = [[NSBundle mainBundle] pathForResource:@"myPlist" ofType:@"plist"];
        NSMutableDictionary *plistDict;
NSMutableArray *array = [[NSMutableArray alloc]initWithArray:[plistDict objectForKey:@"$objects"]];
        for (int i =0 ; i <array.count; i++)
        {
            if ([[array objectAtIndex:i] isKindOfClass:[NSString class]])
            {
                if ([[array objectAtIndex:i] isEqualToString:@"myString"])
                {
    [[plistDict objectForKey:@"$objects"]replaceObjectAtIndex:i withObject:@"newString"];

    }
    }
    }
     NSString *root = @"/Users/myUser/Desktop/newPlist.plist";
        [plistDict writeToFile:root atomically:YES];

内容的字典已被修改,我没有任何错误,但从未在路径中创建文件。你们中的任何人都知道我在做什么错吗?或者我该如何解决?

【问题讨论】:

  • 检查你的 plistdict 是否包含一些数据??
  • 我 po plistdict 并且不是空的
  • 好的,你检查一下你的newplistfile路径是否存在??
  • 使用接受NSError对象的方法版本,自己找出问题所在。

标签: objective-c xcode plist writetofile


【解决方案1】:

我怀疑您正在写入沙盒位置。实际上不是用户目录。

【讨论】:

  • 该目录在沙盒下是被禁止的,但这是一个疯狂的猜测,因为 OP 没有提到沙盒。
  • 是一个mac应用程序。可以写到任何位置
【解决方案2】:

参考以下代码:-

NSMutableDictionary *plistDict=//assuming your data
        NSString *root = @"/Users/home/Desktop/newPlist.plist";
    if (![[NSFileManager defaultManager]fileExistsAtPath:root])//checking fileexist or not 
    {
//if not exist creating the same 
        [[NSFileManager defaultManager]createFileAtPath:root contents:nil attributes:nil];
        [plistDict writeToFile:root atomically:YES];
    }
    else
    {
    [plistDict writeToFile:root atomically:YES];
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-26
    • 2014-04-14
    • 2013-11-06
    • 1970-01-01
    • 2014-12-24
    • 2014-07-24
    • 2013-10-31
    • 2017-07-19
    相关资源
    最近更新 更多