How can I extract the data from a NSMutabledata structure which returned by NSConection, like this:
<response>
TOKEN=abcdef
</response>
As the question is tagged iPhone i think you mean NSURLConnection and not NSConnection. My answer is based on that assumption.
You will have to implement the NSURLConnectionDelegate methods and get all the data into an NSMutableData object. I hope you have already done this as you have the NSMutableData.
How to get data in the format you want depends on what type of data is being returned by the connection. If its an XML or JSON you will have to use a parser. An XML parser is available in objective c. If it is a sipmle string, you can directly convert it to a string in your connectionDidFinishLoading delegate method and use it:
NSString *receivedDataString = [[NSString alloc] initWithData:receivedMutableData encoding:NSUTF8StringEncoding];
Related
I'm receiving data in XML from a server through a NSData object and I would like to convert this data to a NSMutableArray object.
How do I convert the data from the NSData object (knowing that there should be different objects transferred into the NSMutableArray - UIImages ,CLLocation2D objects, strings,...)
Give you a URL, check it by yourself, it is easy to solve.
Conversion of NSMutableData to JSON or Dictionary in Objective C /iPhone App Program?
The NSMutableData that I get is the response from a webservice,
I can convert it easily to NSString, as there is a method - initWithBytes..
But I see the response to be (null).
Since the response actually is in JSON format, can I conver the NSMutableData into JSON using some method or including some framework. (The protocol for the webservice is written in SOAP)
Thanks in advance..
If you're getting your data as plain JSON in a coherent form, you can use this library on Github (JSONKit) to map your JSON strings to objects/dictionaries.
It's a very convenient and easy way to handle JSON. Advantages this library brings are listed on the Github page.
You can use the well-documented NSJSONSerialization class to do this. It comes with Foundation.
I am unsure of how to separate a NSData object into smaller parts so that I can send it over bluetooth. I believe it is a method similar to this:
- (void)getBytes:(void *)buffer range:(NSRange)range
I do not know what to pass in for the buffer. Do I just pass in a NSMutableData object to hold the bytes that I pull out of the original NSData?
Thanks
You can use the -subdataWithRange: method.
I am pulling down a plist from a URL using an NSURLConnection, which returns an
NSData object. I would like to convert this to an NSArray. Can anyone help me?
I am refactoring a method that currently does a synchronous request to the URL,
using arrayWithContentsOfURL: to get the NSArray.
Thanks!!
Use +[NSPropertyListSerialization dataWithPropertyList:format:options:error:] to convert the data, then check if the result -isKindOfClass:[NSArray class].
Use NSKeyedArchiver for archiving object to data.
NSKeyedUnarchiver for unarchiving object from data:
NSArray *object = [NSKeyedUnarchiver unarchiveObjectWithFile:self.receivedData];
I was wondering whether or not it is possible to use the NSCoder method:
- (void)encodeObject:(id)objv forKey:(NSString *)key
to encode either an instance of NSArray or NSDictionary. If not how do you go about encoding them? I am trying to use NSKeyedArchived / NSKeyedUnarchiver to send data between phones using GameKit. I tried it and cannot seem to retrieve the string I stored in my array. The packet class I made comes through along with the packet type integer, but not the data in the array it seems.
Thanks in advance!
If the array or dictionary is the root object you should do
NSData * encodedData = [NSKeyedArchiver archivedDataWithRootObject:someArray];
or
BOOL success = [NSKeyedArchiver archiveRootObject:someArray toFile:filePath];
If it is an instance variable of a custom class, in the -encodeWithCoder: method should do
[coder encodeObject:someArray forKey:#"someArray"];
and then in the -initWithCoder: method
someArray = [[coder decodeObjectForKey:#"someArray"] retain];
What kind of objects are you storing in the array? Make sure that all objects stored in the array implement the NSCoding protocol.
NSKeyedArchiver/Unarchiver should encode and decode NSArrays and NSDictionaries with no problem. If your packet class you've created implements the NSCoding protocol, you need to explicitly call [encodeObject:myNsArrayforKey:#"stringsArray"] in your -encodeWithCoder: method (assuming that myNsArray is the name of the instance variable in your packet object you want to encode). But then the archiver and NSArray should take care of the rest of it. If you're doing this, it would be helpful to hear more about the layout of your classes and who's calling who when encoding/decoding.