tags:

views:

91

answers:

3

Hi, using iphone sdk 4.0. The callback for an http request gives data as an NSData object

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    // Append the data received to our data
    [theData appendData:data];
}

In my php script on the server i am returning an array as follows

var_dump($array).

How do i get my array back from the NSMutableData object 'theData' obove on my iphone.

Thanks

A: 

PHP outputs text so you will have to read that NSData as NSString and then parse out the array data according to the format specified by var_dump. As a starting point, the following code snippet should print out the array (as text) to your console:

NSString * dump = [[NSString alloc] initWithData:theData
                                        encoding:NSUTF8StringEncoding];

NSLog(@"%@", dump);
[dump release];

As Seamus Campbell points out, there are better ways of doing this. Another option would be to output XML from your PHP script, and then use Cocoa's XML parsing methods to retreive the array.

e.James
thanks, this is the format of the arrayclass userInfo { var $user, $created;}user is a char array, created is a unix timestampthen i add these objects to the array in a loop then return with a var_dump.$userInfo = new userInfo()fill in the details etc then add to array$array[] = $userInfo;after loop var_dump($array);I'm quite new to php so got a bit confused how to return this stuff from the server and then how to reuse in Objective C
tech74
+1  A: 

You have a string describing your array (or maybe several arrays?) stored as a sequence of bytes in your NSMutableData object. In order to turn it back into an array you're going to need to parse the var_dump output, which is likely to be arduous.

If you can find a library (or roll your own code) to return your data in Apple plist format, your task will be much easier: you can use

[NSPropertyListSerialization propertyListFromData:mutabilityOption:format:errorDescription:]

which takes an NSData (or NSMutableData) pointer as its first argument. Try http://code.google.com/p/cfpropertylist/ for a starting point.


From the example code at the cfpropertylist page:

$plist = new CFPropertyList();
$td = new CFTypeDetector();  
$guessedStructure = $td->toCFType( $array );
$plist->add( $guessedStructure );
// and then return the plist content with
$plist->toXML()

and in your iOS code:

NSString *errorString = nil;
NSArray *array = [[NSPropertyListSerialization 
                    propertyListFromData:theData
                    mutabilityOption:NSPropertyListImmutable
                    format:nil
                    errorDescription:&errorString] retain];
Seamus Campbell
+1  A: 

I would likely use YAJL on iOS, and $var = json_encode($array); in the PHP. Then in the iOS, I would parse that content from the NSData input like:

YAJLParser *parser = [[YAJLParser alloc] initWithParserOptions:YAJLParserOptionsAllowComments | YAJLParserOptionsCheckUTF8];
parser.delegate = [[[MyArrayParserDelegate alloc] init] autorelease];
[parser parse:data];
NSArray *thePhpArrayReceived = parser.delegate.resultantArray;

Please check out how to structure the delegate, and get YAJL here : Get YAJL + Readme

Heat Miser