jsonmodelios-universal-framework

"Bad property protocol declaration" when trying to initialize model class which is in framework


I have been stretching my head from two days to solve this issue.

I have created iOS universal framework which contains my model classes derived from JSONModel. For example,

@protocol XYZ
@end

@interface XYZ : JSONModel
@property(nonatomic,strong) NSString * name;
@end

Now, whenever I use this "Framework" in other project and try to initialize "XYZ" model class with dictionary,

NSError* err = nil;
XYZ * xyz = [[XYZ alloc] initWithDictionary:jsonDictionary error:&err];

it crashes saying "Bad property protocol declaration".

It is working fine if I do not use framework and put those model class directly in my project. Don't know why such a wired behavior.

I have been looking badly for solution from two days and lots of waste of time. I can see this issue is also raised in github but no any answer from developers. It is very frustrating and even I can't drop JSONModel at this very mature stage of my project. I am having so many model classes and very complex structures that I cannot switch to another library.

Please. Any help would be greatly appreciated. Thank you in advance.


Solution

  • It seems that model classes that was in framework was not loaded by runtime before it was being initialized with dictionary because it was in framework, that's why in following code

    //few built-in transformations
    -(id)__transform:(id)value forProperty:(JSONModelClassProperty*)property error:(NSError**)err
    {
        Class protocolClass = NSClassFromString(property.protocol);
        if (!protocolClass) {
    
            //no other protocols on arrays and dictionaries
            //except JSONModel classes
            if ([value isKindOfClass:[NSArray class]]) {
                @throw [NSException exceptionWithName:@"Bad property protocol declaration"
                                           reason:[NSString stringWithFormat:@"<%@> is not allowed JSONModel property protocol, and not a JSONModel class.", property.protocol]
                                         userInfo:nil];
            }
            return value;
        }
    
        ...........
    }
    

    "protocolClass" was Nil and the error was being thrown.

    Solution was to simply add "-Objc" flag in Other linkers flag, so that classes can be loaded by runtime from static library before they get used.

    Hope this helps to others also.