测试代码如下:
int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSArray* animals = [NSArray arrayWithObjects:@"dog",@"cat",nil];
NSArray* operations = [NSArray arrayWithObjects:@"windows",@"Mac OS X",nil];
NSArray* books = [NSArray arrayWithObjects:@"C++",@"C",nil];
NSString * arraypath = @"/Volumes/VMware Shared Folders/VMShare/array.txt";
[animals writeToFile:arraypath atomically:YES];
NSDictionary* Dict = [NSDictionary dictionaryWithObjectsAndKeys:animals,@"animal",operations,@"operation system",books,@"programming",nil];
NSString * dictpath = @"/Volumes/VMware Shared Folders/VMShare/dict.txt";
[Dict writeToFile:dictpath atomically:YES];
[pool release];
return 0;
}
array.txt文件的内容
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<string>dog</string>
<string>cat</string>
</array>
</plist>
dict.txt文件的内容
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>animal</key>
<array>
<string>dog</string>
<string>cat</string>
</array>
<key>operation system</key>
<array>
<string>windows</string>
<string>Mac OS X</string>
</array>
<key>programming</key>
<array>
<string>C++</string>
<string>C</string>
</array>
</dict>
</plist>
可见生成的是plist文件,<plist></plist>的第一个标志指定了类型。