1

following code is copied from Stig Brautaset JSON library - I removed the comments to be more clear.

@interface NSString (NSString_SBJSON)

- (id)JSONFragmentValue;
- (id)JSONValue;

@end

I have a crash when I call:

NSString *jsonString = [request responseString];
...
NSDictionary *results = [jsonString JSONValue];//here is the crash with invalid selector.

I added this:

if (![jsonString respondsToSelector:@selector(JSONValue)]) 
{
   NSLog(@"fix this!!!\n");
}

and the message is shown. Do you have any idea why this function in not called?

Thanks!

EDIT: I compile JSON framework as a static lib. The call is made in the same lib. When I compile it as exe it runs without any problem.

Mircea Ispas
  • 20,260
  • 32
  • 123
  • 211
  • is your framework added and copied in compile time? see http://stackoverflow.com/questions/6972904/adding-a-framework-to-xcode-4 – Marek Sebera Oct 21 '11 at 20:18
  • @Marek Sebera I compile JSON framework as a static lib. The call us made in the same lib. – Mircea Ispas Oct 21 '11 at 20:21
  • try look up definition of NSString extending code, and check everything is just fine. – Marek Sebera Oct 21 '11 at 20:22
  • @Marek Sebera Is weird that when I compile same code as executable instead of static library it's working without any problem. – Mircea Ispas Oct 21 '11 at 20:23
  • try to check steps to configure static library use in this article: http://blog.carbonfive.com/2011/04/04/using-open-source-static-libraries-in-xcode-4/ – Marek Sebera Oct 21 '11 at 20:28
  • The code is added in a bigger static library that works without any problem. I use bjam to build the project, so the standard configuration steps can't helm me. Thanks for help – Mircea Ispas Oct 21 '11 at 20:30

1 Answers1

1

You need to both link your static library and your final executable with the -ObjC linker option.

The -JSONValue method is implemented as a category on the NSString class in the file NSString+SBJSON.m file. Because of how the dynamic runtime works in Objective-C, categories defined in static libraries don't get loaded up unless you link with the -Objc flag. If the category doesn't get loaded, then dynamic binding fails, and you get the dreaded "doesn't respond to selector" exception.

Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589