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];
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.
I have a NSDictionary in which I wanted to store in my core data model. How can I do this?
Set the attribute to Transformable and this serializes it to and from NSData automatically for you. No need to manually archive as in the other answer.
Use NSKeyedArchiver and store it in data field in your DB :
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:yourDictionary];
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];
A web service provides data in the form of a plist. After the download, I have all of the data in an NSData object, which I want to convert to an NSDictionary. Right now the only way I know of to do that without parsing it by hand (yuck) is this:
static NSString *fileName = #"tempFile";
[data writeToFile: fileName atomically: NO];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile: fileName];
I'm surprised NSDictionary doesn't have a dictionaryWithData: method. I wouldn't even mind converting the NSData to an NSString first, if dictionaryWithString: existed. Clearly the SDK is doing something similar under the hood, but it isn't exposed to developers.
Can anyone suggest a better way to do this? One of the files I need to convert can potentially be pretty big, and doing the write-to-file+read-from-file operations could be a bit slow.
Use one of the following:
+[NSPropertyListSerialization propertyListFromData:mutabilityOption:format:errorDescription:]
+[NSPropertyListSerialization propertyListWithData:options:format:error:]
The former is available in iOS 2.0+ but has been deprecated in 4.0.
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.