写在plist上的问题

时间:2011-05-02 08:48:36

标签: iphone objective-c

我正在向这样的plist写一些数据(字符串):

NSError *error = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *pathProduct = [documentsDirectory stringByAppendingPathComponent:@"unicurd.plist"];           

NSFileManager *fileManager = [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath:pathProduct])
    {
    NSString *bundlePath=[[NSBundle mainBundle] pathForResource:@"unicurd" ofType:@"plist"];
        [fileManager copyItemAtPath:bundlePath toPath:pathProduct error:&error];

    }


    NSString *recipiBookMark= pageURL;

    NSMutableDictionary *recipieBM = [[NSMutableDictionary alloc] init];
    [recipieBM setObject:recipiBookMark forKey:@"BookMarks"];

    BOOL filewrite = [recipieBM writeToFile:@"unicurd.plist" atomically:YES];
    if(filewrite)
    {
        //do somthing
    }
}

但它不是写在档案上。 filewrite的值显示YES,但在plist中它只显示item0和值字段为空白。

2 个答案:

答案 0 :(得分:1)

试试这个..

 [recipieBM writeToFile: pathProduct atomically:YES];    

编辑:

// Create the new dictionary that will be inserted into the plist.
NSMutableDictionary *nameDictionary = [NSMutableDictionary dictionary];
[nameDictionary setValue:@"John Doe" forKey:@"fullName"];
[nameDictionary setValue:@"555 W 1st St" forKey:@"address"];

// Open the plist from the filesystem.
NSMutableArray *plist = [NSMutableArray arrayWithContentsOfFile:@"/path/to/file.plist"];
if (plist == nil) plist = [NSMutableArray array];
[plist addObject:nameDictionary];
[plist writeToFile:@"/path/to/file.plist" atomically:YES];

答案 1 :(得分:1)

您应该写入从文档目录

获取的文件路径
if(![fileManager fileExistsAtPath:pathProduct])
    {
    NSString *bundlePath=[[NSBundle mainBundle] pathForResource:@"unicurd" ofType:@"plist"];
        [fileManager copyItemAtPath:bundlePath toPath:pathProduct error:&error];

    }


    NSString *recipiBookMark= pageURL;

    NSMutableDictionary *recipieBM = [[NSMutableDictionary alloc] init];
    [recipieBM setObject:recipiBookMark forKey:@"BookMarks"];

    BOOL filewrite = [recipieBM writeToFile:pathProduct atomically:YES];
    if(filewrite)
    {
        //do somthing
    }
}