如何格式化 PHP 中的列表以用作 Objective C 中的 NSArray?

发布于 2024-12-14 21:32:37 字数 1183 浏览 0 评论 0原文

所以我对 PHP 是一个全新的人。事实上,我对此一无所知。我正在开发一个 iPhone 应用程序,旨在从服务器获取项目列表。我的 PHP 脚本实际上只是一个“echo”命令。

我想知道在 PHP 上格式化我的列表的最佳方式是什么,以便它看起来尽可能接近(如果不相同)目标 C 中的 NSArray。

例如,如果我的列表是: AAA、BBB、CCC、DDD

通过在 obj-C 中运行这个:

NSArray *myAwesomeArray = [[NSArray alloc] initWithObjects: @"AAA", @"BBB", @"CCC", @"DDD", nil];

我会得到这个,这就是我想要的:

myAwesomeArray IS (
AAA,
BBB,
CCC,
DDD
)

当像这样使用 PHP 执行此操作时(再次,可能是出于该目的的错误格式):

echo "AAA, BBB, CCC, DDD"

而这在 objc 方面:

NSString *urlString =  @"http://......";

NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
[request setTimeoutInterval:60.0];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

NSString *myPHPArray = [[NSString alloc] initWithData:[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil] encoding:NSUTF8StringEncoding];

NSArray *myArray = [myPHPArray componentsSeparatedByString:@","];

我正在得到这个myArray 的 objc:

myArray is (
AAA,
" BBB",
" CCC",
" DDD "
)

其中有点混乱...

感谢任何帮助。谢谢!

So I am a totally new to PHP. In fact, I know nothing about it. I am developing an iPhone app and aiming to get a list of items from a server. My PHP script is literally just a "echo" command.

I was wondering what would be the best way to format my list on PHP so it will look as close as possible (if not identical) to an NSArray in objective C.

So for example if my list is: AAA, BBB, CCC, DDD

By running this in obj-C:

NSArray *myAwesomeArray = [[NSArray alloc] initWithObjects: @"AAA", @"BBB", @"CCC", @"DDD", nil];

I will get this, which is what I want:

myAwesomeArray IS (
AAA,
BBB,
CCC,
DDD
)

When doing this with PHP like this (again, probably the wrong formatting for that purpose):

echo "AAA, BBB, CCC, DDD"

And this on the objc side:

NSString *urlString =  @"http://......";

NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
[request setTimeoutInterval:60.0];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

NSString *myPHPArray = [[NSString alloc] initWithData:[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil] encoding:NSUTF8StringEncoding];

NSArray *myArray = [myPHPArray componentsSeparatedByString:@","];

I am getting this in objc for myArray:

myArray is (
AAA,
" BBB",
" CCC",
" DDD "
)

which is kind of messed up...

Appreciate any help. thanks!

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

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

发布评论

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

评论(3

独守阴晴ぅ圆缺 2024-12-21 21:32:38

您需要采用 PHP 和 iOS 都使用的通用协议。我推荐 JSON。 PHP 有 JSON 支持,现在 iOS 5 本机也支持 JSON(或者您可以使用第三方库之一)。只需按照您想要的结构排列数据,将其编码为 JSON,传输它,然后在 iOS 上将 JSON 解码为 NSArray 和 NSDictionaries。

You need to adopt a common protocol that both PHP and the iOS speak. I recommend JSON. There is JSON support for PHP, and now JSON support in iOS 5 native (or you can use one of the third party libraries). Simply arrange your data in the structures you want, encode it into JSON, transmit it, and have the JSON decode on iOS into NSArrays and NSDictionaries.

岁吢 2024-12-21 21:32:38

您应该使用 XML。如果您使用 plist 格式,您可以使用 NSArray *foo = [responseString proprietyList];或类似的东西。或者你可以使用 NSXMLParser ;)

You should use a XML. If you use plist format you can use NSArray *foo = [responseString proprietyList]; or something like this. Or you can use an NSXMLParser ;)

£冰雨忧蓝° 2024-12-21 21:32:37

从您对 PHP 知之甚少的评论来看,这是一个 Web 服务的基本示例,可以返回连接到 MySQL 数据库的 XML 或 JSON。同样非常基础,但它是一个有效的示例,应该很容易让您入门。

iOS 和 PHP 都支持 XML 或 JSON,您应该可以轻松找到其中的示例。我还建议查看 Web 服务协议以更好地了解可用服务的类型。

From your comment about knowing very little about PHP, this is a basic example of a webservice that can return either XML or JSON that connects to a MySQL Database. Again very basic but is a working example and should be easy enough to get you started.

XML or JSON are supported in both iOS and PHP and you should have no problems finding examples on either. I would also suggest looking at Webservice Protocols to better understand the types of services available.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文