1

两天以来,我一直在努力解决这个问题。

我创建了 iOS 通用框架,其中包含从 JSONModel 派生的模型类。例如,

@protocol XYZ
@end

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

现在,每当我在其他项目中使用这个“框架”并尝试用字典初始化“XYZ”模型类时,

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

它崩溃说“坏属性协议声明”。

如果我不使用框架并将这些模型类直接放在我的项目中,它工作正常。不知道为什么会有这样的有线行为。

两天以来,我一直在寻找解决方案,浪费了大量时间。我可以看到这个问题也在 github 中提出,但开发人员没有任何答案。这非常令人沮丧,甚至在我项目的这个非常成熟的阶段我也不能放弃 JSONModel。我有这么多模型类和非常复杂的结构,我无法切换到另一个库。

请。任何帮助将不胜感激。先感谢您。

4

1 回答 1

1

似乎框架中的模型类在使用字典初始化之前没有被运行时加载,因为它在框架中,这就是为什么在下面的代码中

//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”为 Nil,错误被抛出。

解决方案是在其他链接器标志中简单地添加“-Objc”标志,以便运行时可以在使用之前从静态库中加载类。

希望这对其他人也有帮助。

于 2015-09-14T09:58:28.233 回答