获取远程 plist 文件,在 UITableView 中使用

发布于 2024-08-23 15:34:01 字数 633 浏览 2 评论 0原文

我已经成功创建了一个应用程序,它从捆绑的 .plist 文件中读取内容并在 UITableView 中显示内容。我想将该 plist 移动到外部站点,因为它经常更新。更好的是,我希望用 PHP 生成它。我已经让服务器端工作了,但是 Objective C 让我头疼...

我的代码过去是这样读的:

NSString *myfile = [[NSBundle mainBundle] pathForResource:@"notices" ofType:@"plist"];

根据各种谷歌搜索,我的代码现在应该看起来像这样:

NSString *myfile = [NSDictionary dictionaryWithContentsOfURL:[NSURL URLWithString:@"http://localhost/plistgen1.php"]];

显然这是行不通的,将 NSString 与 NSDictionary 混合,但我尝试过(但失败了)让它正常工作。有没有人有解决方案,或者有不同的方法来解决这个问题?我使用的数据位于 mysql 服务器上,plistgen1.php 只是“填充 plist 文件中的空白”并回显它......

我可能都弄错了,不要开枪打我:)

I have successfully created an app that reads from a bundled .plist file and displays the contents in a UITableView. I would like to move that plist to an external site, as it is updated frequently. even better, I would love to have it generated with PHP. I have got the server side working, but the Objective C is giving me a headache...

My code used to read like this:

NSString *myfile = [[NSBundle mainBundle] pathForResource:@"notices" ofType:@"plist"];

according to various google searches, my code should now look something like this:

NSString *myfile = [NSDictionary dictionaryWithContentsOfURL:[NSURL URLWithString:@"http://localhost/plistgen1.php"]];

Obviously this is not going to work, mixing NSString with NSDictionary, but I have tried (and failed) to get it to work properly. does anyone have a solution, or a different way to approach the problem? The data I am using is on a mysql server, and the plistgen1.php just "fills in the blanks" in the plist file and echoes it out...

I may have got this all wrong, don't shoot me :)

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

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

发布评论

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

评论(1

桃扇骨 2024-08-30 15:34:01

第一种情况(“我的代码过去是这样读的”)为您提供了捆绑的 plist 文件的本地文件系统路径,并将其保存到变量 myfile 中。在第二种情况下,您从服务器下载 plist 文件的内容并从中创建字典。

在将文件路径分配给 myfile 后,您可能有一些代码将该文件的内容读取到 NSDictionary 中。您需要用第二个示例替换对 myfile 的赋值和该语句。

这样的内容替换:

// determine filesystem path to bundled plist file
NSString *myfile = [[NSBundle mainBundle] pathForResource:@"notices" ofType:@"plist"];
// read contents of that plist file and parse them into a dictionary
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:myfile];

所以你可以用

// download plist file contents from URL and parse them into a dictionary
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfURL:[NSURL URLWithString:@"http://localhost/plistgen1.php"]];

The first case ("My code used to read like this") gives you the local filesystem path to the bundled plist file and saves it to the variable myfile. In the second case you're downloading the plist file's contents from the server and creating a dictionary from that.

You probably have some code after the assignment of the file path to myfile that reads the contents of that file into an NSDictionary. You need to replace both the assignment to myfile and that statement with the second example.

So you'd replace something like this:

// determine filesystem path to bundled plist file
NSString *myfile = [[NSBundle mainBundle] pathForResource:@"notices" ofType:@"plist"];
// read contents of that plist file and parse them into a dictionary
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:myfile];

With this:

// download plist file contents from URL and parse them into a dictionary
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfURL:[NSURL URLWithString:@"http://localhost/plistgen1.php"]];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文