【问题标题】:Reading a .plist file that's hosted online读取在线托管的 .plist 文件
【发布时间】:2015-05-16 22:00:41
【问题描述】:

我正在开发一个需要每隔几个月更新一次 .plist 文件的应用程序,我不会为此每次都重新提交该应用程序,因此他们是一种在线托管 .plist 文件的方法,并且有应用程序调用它进行更新吗?我尝试使用 NSURLConnection (或者我没有正确设置它)但是没有用......还有其他想法吗?这是我正在使用的示例文件http://www.iphonedevcentral.com/wp-content/uploads/2009/07/TestData.plist 一如既往地提前感谢,这是我的代码:

NSDictionary *mainDictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"NewsSources" ofType:@"plist"]];

如何更改代码以便访问在线托管的文件?

【问题讨论】:

  • 为什么NSURLConnection 不起作用?如果操作正确,它应该可以工作。使用NSURLConnection 尝试更新您的问题。
  • 对于我使用的 NSURLConnection:'NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfURL:iphonedevcentral.com/wp-content/uploads/2009/07/TestData.plist]; NSArray *array = [NSArray arrayWithContentsOfURL:iphonedevcentral.com/wp-content/uploads/2009/07/TestData.plist];'
  • 正如我所说,更新您的问题。不要在 cmets 中发布代码。
  • 它不会让我更新评论,因为我是新人...
  • 并不是没有答案。我们希望帮助您改进您的问题,让别人更容易准确地回答。请参阅this,了解有关我们正在努力实现的目标的更多信息。我个人不知道你的问题的答案,但我敢肯定有人知道。如果您可以通过使您的问题高质量来让我们的生活更轻松,那么我们可以通过高质量的答案让您的生活更轻松,并且每个人都会赢:)

标签: objective-c info.plist


【解决方案1】:

您的地址示例中的 .plist 是顶级的 NSArray,而不是 NSDictionary。如果您尝试使用它,如果您想要NSDictionary,您将失败。

它的内容清楚地表明了这一点:

<?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> <================= *** HERE ***
    <dict>
        <key>title</key>
        <string>Batman Begins</string>
        <key>coverImage</key>
        <string>batman_begins.png</string>
        <key>featureLength</key>
        <integer>140</integer>
        <key>releaseDate</key>
        <date>2008-07-08T23:32:07Z</date>
    </dict>
    <dict>
        <key>title</key>
        <string>The Dark Knight</string>
        <key>coverImage</key>
        <string>dark_knight.png</string>
        <key>featureLength</key>
        <integer>152</integer>
        <key>releaseDate</key>
        <date>2008-12-10T00:32:07Z</date>
    </dict>
    <dict>
        <key>title</key>
        <string>The Prestige</string>
        <key>coverImage</key>
        <string>prestige.png</string>
        <key>featureLength</key>
        <integer>130</integer>
        <key>releaseDate</key>
        <date>2007-02-21T00:32:07Z</date>
    </dict>
</array>
</plist>

你可以使用:

NSArray *array = [NSArray arrayWithContentsOfURL:[NSURL URLWithString:@"http://www.iphonedevcentral.com/wp-content/uploads/2009/07/TestData.plist"]];

如果 plist(你想使用)真的是顶级的 NSDictionary,你可以使用类似的方法:

NSDictionary *dict = [NSDictionary dictionaryWithContentsOfURL:[NSURL URLWithString:@"linkToNSDictionaryTopLevel.plist"]];

【讨论】:

  • 我刚刚阅读了 NSArray 和 NSDictionary 的文档和方法。
  • 对我来说仍然是中文的兄弟...但现在我可以看到我哪里出错了谢谢!
猜你喜欢
  • 2012-02-04
  • 1970-01-01
  • 2016-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多