访问应用程序包中的 plist 返回 null

发布于 2024-11-28 04:26:52 字数 500 浏览 0 评论 0原文

我正在使用之前成功使用过的一段代码将 plist 加载到数组中:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"species_names" ofType:@"plist"];
NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];

initWithContentsOf 文件失败(数组为空,表明它无法在应用程序包中找到 plist)。

在 iPhone 模拟器下,我检查了第一行创建的路径是否指向应用程序文件(确实如此),并使用 Finder 上下文菜单“显示包内容”向自己证明“species_name.plist”实际上位于捆绑包——其长度表明它包含内容,因此它应该可以工作(并且在我编写的其他iOS应用程序中也有)。欢迎提出建议...

[env Xcode 4.2 beta,iOS 4.3.2]。

I'm using a piece of code I've used before with success to load a plist into an array:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"species_names" ofType:@"plist"];
NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];

The initWithContentsOf File is failing (array is null, suggesting it can't find the plist in the app bundle).

Under the iPhone simulator I've checked that the path created by the first line points to the app file (it does), and used the Finder context menu "Show package contants" to prove to myself that "species_name.plist" is actually in the bundle--with a length that suggests it contains stuff, so it should work (and has in other iOS apps I've written). Suggestions most welcome...

[env Xcode 4.2 beta, iOS 4.3.2].

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

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

发布评论

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

评论(3

神妖 2024-12-05 04:26:52

您应该使用 NSDictionary 实例的 initWithContentsOfFile: 方法,而不是 NSArray
这是示例:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"species_names" ofType:@"plist"];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:filePath];
NSArray *array = [NSArray arrayWithArray:[dict objectForKey:@"Root"]];

或者这个:

NSString *errorDesc = nil;
NSPropertyListFormat format;
NSString    *path = [[NSBundle mainBundle] pathForResource:@"startups" ofType:@"plist"];
NSData      *plistXML = [[NSFileManager defaultManager] contentsAtPath:path];
NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization                     
                                          propertyListFromData:plistXML
mutabilityOption:NSPropertyListMutableContainersAndLeaves
                                          format:&format
                                          errorDescription:&errorDesc];
NSArray *array = [NSArray arrayWithArray:[temp objectForKey:@"Root"]];

编辑

您可以将 NSArrayinitWithContentsOfFile: 方法与使用 NSArraywriteToFile:atomically: 方法创建的文件一起使用。使用 writeToFile:atomically: 创建的 Plist 具有以下结构:

<plist version="1.0">
<array>
    ...Content...
</array>
</plist>

在 XCode 中创建的 Plist 具有以下结构:

<plist version="1.0">
<dict>
    ...Content...
</dict>
</plist>

这就是为什么 NSArrayinitWithContentsOfFile: 方法> 不起作用。

You should use initWithContentsOfFile: method of NSDictionary instance, not NSArray.
Here is sample:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"species_names" ofType:@"plist"];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:filePath];
NSArray *array = [NSArray arrayWithArray:[dict objectForKey:@"Root"]];

Or this:

NSString *errorDesc = nil;
NSPropertyListFormat format;
NSString    *path = [[NSBundle mainBundle] pathForResource:@"startups" ofType:@"plist"];
NSData      *plistXML = [[NSFileManager defaultManager] contentsAtPath:path];
NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization                     
                                          propertyListFromData:plistXML
mutabilityOption:NSPropertyListMutableContainersAndLeaves
                                          format:&format
                                          errorDescription:&errorDesc];
NSArray *array = [NSArray arrayWithArray:[temp objectForKey:@"Root"]];

EDIT

You can use initWithContentsOfFile: method of NSArray with file created with writeToFile:atomically: method of NSArray. Plist created with writeToFile:atomically: has this stucture:

<plist version="1.0">
<array>
    ...Content...
</array>
</plist>

And Plist created in XCode has this stucture:

<plist version="1.0">
<dict>
    ...Content...
</dict>
</plist>

Thats why initWithContentsOfFile: method of NSArray dosnt work.

悲歌长辞 2024-12-05 04:26:52

此类型的 plist

在此处输入图像描述

或(基本上两者相同)

在此处输入图像描述

可以这样读取:

NSString *file = [[NSBundle mainBundle] pathForResource:@"appFeatures" ofType:@"plist"];
NSArray *_arrFeats = [NSArray arrayWithContentsOfFile:file];
NSLog(@"%d", _arrFeats.count);

其中 appFeaturesplist.
(iOS 6.1)

plist of this type:

enter image description here

OR (basically both are same)

enter image description here

Can be read like this:

NSString *file = [[NSBundle mainBundle] pathForResource:@"appFeatures" ofType:@"plist"];
NSArray *_arrFeats = [NSArray arrayWithContentsOfFile:file];
NSLog(@"%d", _arrFeats.count);

Where appFeatures is name of the plist.
(iOS 6.1)

笙痞 2024-12-05 04:26:52

尝试这个简单的方法:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self plistData];
}


- (void)plistData
{
    NSString *file = [[NSBundle mainBundle] pathForResource:@"appFeatures" ofType:@"plist"];
    NSArray *messages = [NSArray arrayWithContentsOfFile:file];
    NSLog(@"%d", messages.count);

    for (int i=0; i<messages.count; i++)
    {
        NSString *data = [messages objectAtIndex:i];
        NSLog(@"\n Data = %@",data);
    }
}

Try this simple metod:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self plistData];
}


- (void)plistData
{
    NSString *file = [[NSBundle mainBundle] pathForResource:@"appFeatures" ofType:@"plist"];
    NSArray *messages = [NSArray arrayWithContentsOfFile:file];
    NSLog(@"%d", messages.count);

    for (int i=0; i<messages.count; i++)
    {
        NSString *data = [messages objectAtIndex:i];
        NSLog(@"\n Data = %@",data);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文