I have a script which at one point iterates over an NSArray of objects. I’m only interested in those that are NSDictionary, so my initial thought was to try
if (obj's isKindOfClass:(NSDictionary's class)) then
Alas, although that worked with other classes (e.g., NSData), it seems that the objects ( NSCFDictionary’s underneath) return false for that. Why is a mystery to me.
It then occurred to me to try seeing whether the class responds to “objectForKey” (which is actually what I’m calling on the next line of the script).
So I came up with
if (obj's class's respondsToSelector:"objectForKey:") then -- do something end if
Alas, that never returns true either, so I’m wondering if I’ve got the syntax muddled up.
A couple of
- in this array there’s 40 some objects, 34 or so of them are NSDictionary/NSCFDictionary, the others are either NSString, NSData or NSNumber. I suppose I could use a bunch of is not conditionals, but that seems both fragile and inefficient.
- If I just leave out the condition and go straight to the next line and call ‘objectForKey’ on the object (regardless of what class it is), the script does in fact succeed; AppleScript seems to ignore the underlying unrecognized selector sent to instance error messages (possibly cos the repeat is wrapped in a try block); however, I’d like to produce something more robust that doesn’t just trample over the errors, but takes steps to avoid them.
TIA for any thoughts!