NSObject的属性何时可以为null

时间:2018-06-11 07:57:29

标签: ios

我有一个模特翻译。它会显示AModel的属性,如果BModel有,则将值复制到BModel相同的属性。现在我有一个崩溃报告显示该属性为null。那太奇怪了。该属性来自一个属性列表,它是null

以下是信息:

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<XXXXXX 0x1594051a0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key (null).

+ (instancetype)convertSourceObject:(id)sourceObject {
if (!sourceObject) {
    return nil;
}

id destination_object = [[self alloc] init];

uint destination_properties_count = 0;

objc_property_t *destination_properties = class_copyPropertyList([self class], &destination_properties_count);

for (int i = 0; i < destination_properties_count; i++) {

    objc_property_t destination_property = destination_properties[i];

    uint source_attributes_count = 0, destination_attributes_count = 0;

    objc_property_attribute_t *destination_attributes = property_copyAttributeList(destination_property, &destination_attributes_count);

    const char *property_char_name = property_getName(destination_property);
    NSString *property_name = [NSString stringWithUTF8String:property_char_name];

    objc_property_t source_property = class_getProperty([sourceObject class], property_char_name);

    if (source_property && ![ignorePropertyNames() containsObject:property_name]) {
        objc_property_attribute_t *source_attributes = property_copyAttributeList(source_property, &source_attributes_count);

        NSString *source_ivar_type = @"";
        NSString *destination_ivar_type = @"";

        for (int i = 0; i < source_attributes_count; i++) {
            if (strcmp(source_attributes[i].name, "T") == 0) {
                source_ivar_type = [[NSString stringWithUTF8String:source_attributes[i].value] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"@\""]];

                break;
            }
        }

        for (int i = 0; i < destination_attributes_count; i++) {
            if (strcmp(destination_attributes[i].name, "T") == 0) {
                destination_ivar_type = [[NSString stringWithUTF8String:destination_attributes[i].value] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"@\""]];
                break;
            }
        }

        if ([self isPropertySetType:source_ivar_type]) {
            id source_value = [sourceObject valueForKey:property_name];
            if ([source_value isKindOfClass:[NSArray class]]) {
                NSArray *destination_array = [self arrayConvert:(NSArray*)source_value];
                if (destination_array) {
                    [destination_object setValue:destination_array forKey:property_name];
                }

            } else if ([source_value isKindOfClass:[NSDictionary class]]) {
                NSDictionary *destination_dict = [self dictionaryConvert:(NSDictionary *)source_value];
                if (destination_dict) {
                    [destination_object setValue:destination_dict forKey:property_name];
                }
            }

        } else {
            if ([destination_ivar_type isEqualToString:source_ivar_type]) {
                id source_value = [sourceObject valueForKey:property_name];
                if (source_value) {
                    [destination_object setValue:source_value forKey:property_name];
                }

            } else {
                id source_value = [sourceObject valueForKey:property_name];

                id destination_value = [NSClassFromString(destination_ivar_type) convertSourceObject:source_value];
                if (destination_value) {
                    [destination_object setValue:destination_value forKey:property_name];
                }
            }
        }
        free(source_attributes);
    } else {
        continue;
    }
    free(destination_attributes);
}
free(destination_properties);
return destination_object;

}

1 个答案:

答案 0 :(得分:1)

错误意味着:您尝试读取名为“null”AModel属性的属性,但它不会在AModel中退出。

您应该在Model Class中覆盖valueForUndefinedKey,调试UndefinedKey。

检查你的代码。似乎发生在

id source_value = [sourceObject valueForKey:property_name];

NSLog调试property_name,看看你得到了什么。