Pregunta

I am trying to use the JSONKit library and have included the source files in my project. I have included the JSONKit.h header so that the included category gives NSDictionary objects the JSONData and JSONString selectors.

  NSMutableDictionary* root = [NSMutableDictionary dictionary];
  [root setValue:@"CONNECT" forKey:@"command"];
  NSData* data = [root JSONData];

But at runtime I get the following error:

 -[__NSCFDictionary JSONString]: unrecognized selector sent to instance 0x784b870

Why is the category not adding the appropriate methods to NSDictionary objects?

¿Fue útil?

Solución

I'm answering my own question because I just figured it out.

My code is part of a static library that is being referenced from another iOS application. When including a static library that exports categories, the application that is using the category must be linked with the "-ObjC" linker flag.

Otros consejos

While the accepted answer resolved the issue for me, understanding why it did (and why it might not) is definitely useful. For a more detailed look at this issue and possible solutions, see this post.

It's a bit of a n00b error, but I ran into exactly the same exception when I imported the JSONKit.h and JSONKit.m files into my project, but did not tick the box to add it to the target in Xcode.

Open JSONKit.m, show the File Inspector section of the Utilities panel, and in the Target Membership make sure there is a box ticked next to your app.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top