iPhone: Missing method? NSDictionary dictionaryWithData: - iphone

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.

Related

Incompatible Objective C types

I am trying to store the location of my Application's Document Directory in a NSString.
NSString *documentDirectory = [self applicationDocumentsDirectory];
The code compiles without error, but I get the warning that I am trying to initialize a NSString with a NSURL. When I run the app, the app crashes. I tried casting, but it gave me an error. The book I got it off(Head First iPhone Development) says the code should work. The code is in my appDelegate.m file. Any Help will be greatly appreciated.
Regards
It is returning an NSURL as Dave DeLong said. However the easiest way to get round this and convert it to an NSString without changing the method, is setting the string to the NSURL's absoluteString.
NSString *documentDirectory = [[self applicationDocumentsDirectory] absoluteString];
Your -applicationDocumentsDirectory method is returning an NSURL. You want it to return an NSString.
The methods return a NSURL. You must convert the url in NSString.
It's very easy. Use a method in NSURL class.

Losing NSString

I'm copying an NSString from an NSDictionary into a local NSString using [[NSString alloc] initWithString:], processing it (removing some chars) then sending it to an external object.
I've used two methods a [[MyObject alloc] initWithString:] and [myObject setString:]; neither work.
Both of them make my app crash; when I use a breakpoint on the given area I get an empty NSString; I use NSLogs from the start of the NSString until I send it to my object; they all show the string's correct value…
Thank you all for your valuable input :-)
Replace the [[NSString alloc] initWithString:stringBeingCopied] call with [[NSMutableString alloc] initWithString:stringBeingCopied], creating a mutable string, as opposed to an immutable ("normal") string will stop the setString: calls from crashing your app
Immutable strings, being immutable, don't respond to setString: calls - which are NSString mutators. Creating an NSMutableString instead, which implements setString:, will let you modify the string object.
I've used two methods a [[MyObject
alloc] initWithString:] and [myObject
setString:]; neither work.
Show your work! Can't help you without showing more code. In particular, how did you implement initWithString: and setString: on your MyObject class?
Strings are normally immutable. To modify a string, you need a NSMutableString. Either do [string mutableCopy], or do this from inside the initWithString method. Maybe you do this already, but please post your code if possible. I'm afraid it's hard to give any further help without it...

How do I write ALAsset in a NSData

I'm trying to write ALAsset in a NSData and somehow came up with the following code which does not work. What I would like to know is, whether the code is similar to this? Or is there another way of doing it?
NSData *data = [[NSData alloc] initWithContentsOfFile:#"assets-library://asset/asset.MOV? id=100000009&ext=MOV" ];
The defaultRepresentation of an ALAsset has a getBytes:fromOffset:length:error: method. Use that.
There's a space between your "?" and "id=.." is this the problem?
The best way to debug this is to first try opening that file you name (or creating an NSURL and using initWithContentsOfURL as suggested by Max) and then running the debugger, checking whether that succeeds. Right now you're debugging a few things simultaneously, break out that compound statement and figure out what could be going wrong

Has anyone had experience using the Objective-C bindings for YAJL?

http://github.com/gabriel/yajl-objc
I've already tried SBJSON, and while it works, I'm looking into alternative options to improve on parsing speed and memory consumption. Usage of this library doesn't seem to be as straightforward as SBJSON though, and I'm not sure how to begin using yajl.
Something like this:
NSArray *parsed = [data yajl_JSON];
Results in the following error:
-[NSConcreteMutableData yajl_JSON]: unrecognized selector sent to instance 0x5372180
Attempting to parse an NSString object results in the same problem. I can see the interface, but there doesn't seem to be an implementation... Am I not linking into the static lib properly?
Google turns up very little on usage examples. The doc on for the project itself only says the following about generating objects from json data/strings.
#import "NSObject+YAJL.h"
NSData *JSONData = [NSData dataWithContentsOfFile:#"example.json"];
NSArray *arrayFromData = [JSONData yajl_JSON];
NSString *JSONString = #"[\"Test\"]";
NSArray *arrayFromString = [JSONString yajl_JSON];
Which looks pretty much the same as what I've tried. What am I missing here?
You must have missed that part:
Under 'Other Linker Flags' in the Test target, add -ObjC and -all_load (So NSObject+YAJL category is loaded).
I had no problems with using YAJL, until I ran the app in Release mode, then I got the same error as you - turns out, I've only added these flags to Debug mode, not to all of them.
In general, I can recommend YAJL, it's definitely faster than all other alternatives (see benchmark results on my blog).

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