开发者

Objective C - Writing an NSString to a plist

开发者 https://www.devze.com 2022-12-20 05:34 出处:网络
I\'m writing an NSString to a plist file but after its written to the plist, and when I try to open i get the following message

I'm writing an NSString to a plist file but after its written to the plist, and when I try to open i get the following message "This document "mylist.plist" could not be opened XML pa开发者_Go百科rser error: Unexpected character 2 at line 1 Old-style plist parser error: Unexpected';' or '=' after key at line 1"

Here is my code:

NSString *temp = [NSString stringWithFormat:@"%@\n Selection is %@ \n %d for %.2lf = %.2lf", [NSDate date], @"IPC", 2, 42.34, 2 * 42.34]; 
[temp writeToFile:[self getPathName:@"mylist.plist"] atomically:YES]; 

any help would be appreciated.

Thanks,


-[NSString writeToFile...] does not create a plist. It creates a text file. There is no such thing as "writing a string to a plist". Only NSArray and NSDictionary objects can be written to plist files. Those can then contain NSString objects (and other objects, like NSDate and NSData, etc), but what you're asking for is not possible.

For more information, check out the Property List Programming Guide.

Edit: I should clarify what I mean by "creating a plist". When I say that, I'm referring to the XML documents defined by the Apple Plist DTD: http://www.apple.com/DTDs/PropertyList-1.0.dtd


%@\n Selection is %@ \n %d for %.2lf = %.2lf is definitely not in any plist format. If you want to retain the plist format, use +[NSPropertyListSerialization dataFromPropertyList:...] to convert the string into data as a plist, then save the data.

And I don't see a reason to use plist if you're only storing 1 string. You can simply save as a .txt and load it using +[NSString stringWithContentsOfFile:...].

0

精彩评论

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

关注公众号