Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
209 views
in Technique[技术] by (71.8m points)

How to format list in PHP to be used as an NSArray in Objective C?

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...