添加文件到我的Plist - 我想我差不多了

时间:2012-03-26 08:36:44

标签: ios plist save

在我的iOS应用程序中,我创建了一个应该将数据提交到plist中的表单。我已经跟踪了这个链接中的例子 -

  

How to write a data in plist?

现在我觉得我已经做了一切正确,除了我得到一个错误并且它与ARC有关 - 说Xcode - 所以我不确定我使用的例子是否过时或者我是否忘记了什么。

澄清一下 - 我有适当的保存按钮 - (IBAction),它没有给出任何错误,所以我觉得我很好。我认为需要调整的位是将原始的Data.plist复制到文档文件夹中,我认为我想用这个来实现。

我已粘贴下面发生错误的代码。任何帮助都很棒,如果我可以通过发布更多代码来帮助我,请告诉我: - )

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.


NSString *destPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
destPath = [destPath stringByAppendingPathComponent:@"Data.plist"];

// If the file doesn't exist in the Documents Folder, copy it.
NSFileManager *fileManager = [NSFileManager defaultManager];

if (![fileManager fileExistsAtPath:destPath]) {
    NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
    [fileManager copyItemAtPath:sourcePath toPath:destPath];
}

// Load the Property List.
savedReadingsDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:destPath];
}

干杯杰夫

我刚注意到我没有澄清是什么给了我错误:

就是这一行

[fileManager copyItemAtPath:sourcePath toPath:destPath];

,错误是:

  

ARC问题 - 'NSFileManager'没有可见的@interface声明选择器'copyItemAtPath:toPath:'

2 个答案:

答案 0 :(得分:2)

NSFileManager不包含该方法。

[fileManager copyItemAtPath: toPath:];

您应该使用以下方法

- (BOOL)copyItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error NS_AVAILABLE(10_5, 2_0);

答案 1 :(得分:2)

您使用了一种不存在的方法。您只需要将其更改为

[fileManager copyItemAtPath:sourcePath toPath:destPath error:nil];

您也可以传递自定义nil对象,而不是将NSError作为错误传递。请参阅NSFileManager class reference以查看所有允许的方法