简体   繁体   中英

Writing a plist

I am trying to test out writing a dictionary to a plist. The following code does not report any errors, but I cannot find any trace of the file that I supposed wrote. Here is the code snippet:

 NSDictionary *myDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
 @"First letter of the alphabet", @"A",
 @"Second letter of the alphabet", @"B",
 @"Third letter of the alphabet", @"C",
 nil
 ];

I can see the dictionary contents displayed properly with either method calls:

NSLog(@"Here is my partial dictionary %@", myDictionary); 

for (NSString *key in myDictionary)
    NSLog(@"here it is again %@ %@", key, [myDictionary objectForKey:key]);

The following code displays the "succeeded" message when the program is run repeatedly

if ([myDictionary writeToFile: @"myDictionary" atomically:YES ] == NO)
    NSLog(@"write to file failed");
    else
    NSLog(@"write to file succeeded");

even when changing the atomically: argument to NO to not write a temporary file. However, when I search my current directory, or even my entire Mac, I cannot find any file called "myDictionary.plist" or any file with the string "myDictionary".

Isn't the path variable "@myDictionary" supposed to represent the file at the current directory, ie where the class executable resides?

Your code tells the system to write the dictionary on a file, but on what file, what is the path to the file's location? Alekkhya's code does that, it defines a particular directory in the application sandbox(the Documents directory), gets the path to it and writes the file there.

I suggest you read this .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM