Separate NSData into smaller NSMutableData objects - iphone

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.

Related

How to convert XML data into a NSMutableArray

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.

Extract data from NSMutableData

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

merging two data objects

I have to merge two data objects and later separate them.Can anybody suggest me the way to go about it.It's urgent!!!!!!
please respond fast....
Thanks
Have a look at NSMutableData. Specifically, look at the appendData: and appendBytes:Length methods.
You can 'merge' two NSData objects by appending them to an empty NSMutableData object. You can then probably retrieve the data using getBytes:range: and reconstruct your NSData object with dataWithBytes:Length:.
Have a look at this article for working with mutable binary data (includes sample code).

Need to convert NSData to NSArray

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

Encode NSArray or NSDictionary using NSCoder

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.