【问题标题】:"Bad property protocol declaration" when trying to initialize model class which is in framework尝试初始化框架中的模型类时出现“错误的属性协议声明”
【发布时间】:2015-09-12 06:41:13
【问题描述】:

为了解决这个问题,我这两天一直在伸头。

我创建了 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。我有这么多模型类和非常复杂的结构,我无法切换到另一个库。

请。任何帮助将不胜感激。提前谢谢你。

【问题讨论】:

    标签: jsonmodel ios-universal-framework


    【解决方案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”标志,以便运行时可以在使用之前从静态库中加载类。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-17
      • 1970-01-01
      • 2022-11-14
      • 2021-04-27
      • 1970-01-01
      • 2019-07-25
      相关资源
      最近更新 更多