开发者

creating a plist file

I want to write an Array to a plist wich works fine when i manually create the plist in the document folder.

but When I check if the plist exist and if not to create it from the plist in main bundle nothing happen.....

i did it as in the Property List P开发者_高级运维rogramming Guide it is written.

NSString *plistRootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *plistPath = [plistRootPath stringByAppendingPathComponent:@"list.plist"];

if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {

    plistPath = [[NSBundle mainBundle] pathForResource:@"list" ofType:@"plist"];

}

what did I missed?

edit:

I tryed the function in an seperate App and it worked. So I tryed It again:

 NString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
 NSString *plistPath = [rootPath stringByAppendingPathComponent:@"list.plist"]; 

self.ListArray = [NSMutableArray arrayWithContentsOfFile:plistPath]; 
NSLog(@"preSave: %@", [ListArray count]);
NSLog(@"%@", plistPath);

and the Log says:

2011-02-16 16:54:56.832 ListApp[3496:207] .../Library/Application Support/iPhone Simulator/4.0.2/Applications/267F55E4-A46A-42E0-9E1C-EAF26846F75F/Documents/list.plist

but there is no file in the Dir.!


NSString *DocPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

filePath=[DocPath stringByAppendingPathComponent:@"AlarmClock.plist"];

if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) 
{
    NSString *path=[[NSBundle mainBundle] pathForResource:@"AlarmClock" ofType:@"plist"];
    NSLog(@"file path: %@",filePath);
    NSDictionary *info=[NSDictionary dictionaryWithContentsOfFile:path];
    [info writeToFile:filePath atomically:YES];
}

//***Try this...


I tryed the code on an other mac and there it works It's the same project.

can anyone explain me why the plist is created on one mac but not on the other?

on the IPhone also no plist is written?

I guess in both cases this is cause the array I write to the plist is empty:

NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *plistPath = [rootPath stringByAppendingPathComponent:@"list.plist"];  
NSMutableArray *currentArray = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];   
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys: 
            [selectet valueForKey:NAME], @"name",
            nil];   

NSLog(@" DIC. : %@", dictionary);
[currentArray addObject:[dictionary copy]];
NSLog(@" %@", currentArray);
[currentArray writeToFile:plistPath atomically:YES];
[currentArray release];

the dictionary has the entry but currentArray is empty.... And still it works on one mac. but not on the other and on the IPhone.

Edit:

For some reason it worked again.

To be sure I deleted the plist from the document folder and cleared all targets.

And now the Problem as before I can only write the plist when i write the dictionary to it but then i can only save the last one. And reading the plist won't work either.

Can anyone Tell me why this happens and how I can solve it?

Edit 2:

I found the Problem: since X-Code won't create the plist when I write the array with the dictionary to the plist.When I wrote the dictionary to the plist so X-Code creates the plist but with a dictionary Root so I cant write the array to it and I also can't read it in an Array.....

I created a plist in the Resources folder with a Array Root and copy it when it not exist to document folder and now it works.....

isn't it amazing how such easy things can give you such an headache

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜