如何在 iphone 3.0 中将值存储在 Plist 中并在运行时读取?

发布于 2024-08-30 13:57:39 字数 57 浏览 1 评论 0原文

如何在 iphone 3.0 中在 Plist 中存储值并在运行时读取?

提前致谢。

how to store values inside Plist and read while runtime in iphone 3.0?.

Thanks in Advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天赋异禀 2024-09-06 13:57:39

如果您有一个仅包含“标准”数据类型(例如字符串、日期、数字、数组和字典)的数组或字典,则可以使用 -[NSArray writeToFile:atomically:]< 将内容保存到 .plist 文件中/code> 或 -[NSDictionary writeToFile:atomically:]。要读取文件,请使用-initWithContentsOfFile:

请注意,应用程序包在 iPhone OS 设备上不可写,因此您必须将该文件存储在应用程序的 Documents 目录中。

If you have an array or dictionary that contains only "standard" data types like strings, dates, numbers, arrays, and dictionaries, you can save the contents to a .plist file with -[NSArray writeToFile:atomically:] or -[NSDictionary writeToFile:atomically:]. To read the file, use -initWithContentsOfFile:.

Note that the application bundle is not writable on iPhone OS devices so you will have to store the file in your app's Documents directory.

水染的天色ゝ 2024-09-06 13:57:39

该解决方案可以应用于 NSArray 和 NSDictionary。

使用此方法从属性列表创建一个 NSData 并使用 writeToFile 将其保存到磁盘。

[NSPropertyListSerialization dataFromPropertyList:(id)plist
                                           format:(NSPropertyListFormat)format
                                 errorDescription:(NSString **)errorString];

使用此方法从 NSData 读取属性列表。

[NSPropertyListSerialization propertyListFromData:(NSData *)data
                                 mutabilityOption:(NSPropertyListMutabilityOptions)opt
                                           format:(NSPropertyListFormat *)format
                                 errorDescription:(NSString **)errorString];

例子:

NSPropertyListFormat format = 0;
NSString *errorString = nil;

NSDictionary *dataDict = [NSPropertyListSerialization propertyListFromData:data
        mutabilityOption:NSPropertyListMutableContainersAndLeaves
        format:&format errorDescription:&errorString];

if (errorString != nil) {
    NSLog(errorString);
    [errorString release];
}

NSLog(@"got dictionary:%@", dataDict);

errorString = nil;
NSData *data = [NSPropertyListSerialization dataFromPropertyList:dataDict
        format:NSPropertyListXMLFormat_v1_0 errorDescription:errorString];

NSLog(@"plist data:%@", data); // convert to NSString to get <plist>

This solution can be applied to both NSArray and NSDictionary.

Use this method to make a NSData from property list and use writeToFile to persist it to disk.

[NSPropertyListSerialization dataFromPropertyList:(id)plist
                                           format:(NSPropertyListFormat)format
                                 errorDescription:(NSString **)errorString];

Use this method to read property list from NSData.

[NSPropertyListSerialization propertyListFromData:(NSData *)data
                                 mutabilityOption:(NSPropertyListMutabilityOptions)opt
                                           format:(NSPropertyListFormat *)format
                                 errorDescription:(NSString **)errorString];

Example:

NSPropertyListFormat format = 0;
NSString *errorString = nil;

NSDictionary *dataDict = [NSPropertyListSerialization propertyListFromData:data
        mutabilityOption:NSPropertyListMutableContainersAndLeaves
        format:&format errorDescription:&errorString];

if (errorString != nil) {
    NSLog(errorString);
    [errorString release];
}

NSLog(@"got dictionary:%@", dataDict);

errorString = nil;
NSData *data = [NSPropertyListSerialization dataFromPropertyList:dataDict
        format:NSPropertyListXMLFormat_v1_0 errorDescription:errorString];

NSLog(@"plist data:%@", data); // convert to NSString to get <plist>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文