0

I want to see if the results of this call:

NSDictionary *results = [jsonString objectFromJSONString];
id contacts=[[results objectForKey:@"list"] objectForKey:@"Contact"];

Return an array or a dictionary.

I tried this:

    [contactdict isKindOfClass:[JKArray class]];

but JKArray is statically declared in the JSONKit.m file, so the xcode can't see it.

Paul Wand
  • 323
  • 5
  • 12

2 Answers2

9

This is what NSClassFromString is for:

if ([contactDict isKindOfClass:NSClassFromString(@"JKArray")])
{
    // do stuff here
}
Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201
1

You may be able to simply add @class JKArray; to the top of the file where you're making this call. That just tells the compiler that there is a class named JKArray. The actual test happens at runtime of course.

Alternatively, you should be able do this:

[[contactDict className] isEqualToString:@"JKArray"];

or this:

[contactDict isKindOfClass:NSClassFromString(@"JKArray")];
Andrew Madsen
  • 21,309
  • 5
  • 56
  • 97
  • Correction: The actual test happens at link-time, not runtime. – Richard J. Ross III May 19 '12 at 01:56
  • I was referring to the `if ([contactDict isKindOfClass[JKArray class]])` test, which most certainly happens at runtime. You're correct in that the linker will throw an error if JKArray isn't defined in any of the compiled source files being linked. However, as long as it does exist, the program will link and work just fine. You will get one or more compiler warnings ("receiver 'JKArray' is forward class..."). The NSClassFromString or className comparison approaches are the better way to go. – Andrew Madsen May 19 '12 at 17:51