gpt4 book ai didi

iphone - dictionaryWithContentsOfFile 从我的属性列表文件中返回 nil

转载 作者:太空狗 更新时间:2023-10-30 03:34:10 25 4
gpt4 key购买 nike

我正在学习如何使用 plist 文件在我的 iPhone 应用程序中存储数据。我在这个网站上阅读了很多关于 plist 和 dictionaryWithContentsOfFile 的问题,但看不出我做错了什么。

以下行返回 nil(“字典为空”到控制台)。

NSLog(@"The dictionary is %@",[NSDictionary dictionaryWithContentsOfFile:@"myFile"]);

myFile.plist 文件位于资源文件夹中,包含一些字符串:

<?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>name</key>
<string>john</string>
<key>aname</key>
<string>jen</string>
</dict>
</plist>

我是不是漏掉了什么?

最佳答案

从文档...

+ (id)dictionaryWithContentsOfFile:(NSString *)path

Parameters

path

A full or relative pathname. The file identified by path must contain a string representation of a property list whose root object is a dictionary.

Return Value

A new dictionary that contains the dictionary at path, or nil if there is a file error or if the contents of the file are an invalid representation of a dictionary.

因此,您可能需要构造一个到您的 plist 文件的完整路径(或传入一个适当的相对路径。如果“myFile”是一个适当的相对路径,我会感到非常惊讶)。如果您构建了正确的路径,但仍然返回 nil,那么您可能得到了一个无效的 plist 文件。

获取资源文件夹中文件路径的最简单方法如下:

NSString * plistPath = [[NSBundle mainBundle] pathForResource:@"myFile" ofType:@"plist"];

这将为资源文件夹中名为“myFile.plist”的文件构建路径。

关于iphone - dictionaryWithContentsOfFile 从我的属性列表文件中返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3895898/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com