简体   繁体   中英

Extracting array from NSMutableData in http response

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

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];

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM