karajanyp / RWJSONAid

Several Objective-C Categories to avoid crash raised by doesNotRecognizeSelector when sending messages to incompatible instances for Data object in JSON dicticonary.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

RWJSONAid

Description

RWJSONAid is a simple set of Objective-C Categories to avoid crash when sending messages to incompatible object instances from a JSON dicticonary in iOS.

Background

Given following situation:

NSString* someId = jsonDictionary[@"id"];
NSString* completeString = [someid stringByAppendingString:@"-SomeOtherThing"]; //get crash when jsonDictionary[@"id"] is not NSString.

Program crashes when object type is not as assummed!

NSString* someId = jsonDictionary[@"id"];
if ([someId isKindOfClass:[NSString class]]){
  NSString* completeString = [someid stringByAppendingString:@"-SomeOtherThing"];
} 

We always perform defenvise coding to get rid of crashing. But such kind of code snippet could be annoying. Here we have a neat sulution:RWJSONAid.

How it works

forwardingTargetForSelector is override as following to forward message to a correct coresponding object instance.

- (id) forwardingTargetForSelector:(SEL)aSelector{
    if ([self respondsToSelector:aSelector]) {
        return [super forwardingTargetForSelector:aSelector];
    }else{
        if ([someOtherObjectInstance respondsToSelector:aSelector]){
            return someOtherObjectInstance;
        }else if ...{
        }
    }
    return [super forwardingTargetForSelector:aSelector];
}

About

Several Objective-C Categories to avoid crash raised by doesNotRecognizeSelector when sending messages to incompatible instances for Data object in JSON dicticonary.

License:MIT License


Languages

Language:Objective-C 100.0%