iOS UIAlertAction 使用KVC改变“titleT

2019-11-26  本文已影响0人  婼熙之名

报错信息

[<UIAlertAction 0x7fbf48408d30> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key titleTextColor.

原因:iOS 8.3系统以下,UIAlertAction没有titleTextColor这个key值。我们可以使用RunTime验证。

#pragma mark---runtime分析获取UIAlertAction的属性列表和成员变量列表:

-(void)aboutAlert{

    unsignedintcount;// 记录属性个数

    objc_property_t *properties = class_copyPropertyList([UIAlertAction class], &count);

    // 遍历

    NSMutableArray *propertiesArray = [NSMutableArray array];

    for(inti =0; i < count; i++) {

        // objc_property_t 属性类型

        objc_property_tproperty = properties[i];

        // 获取属性的名称 C语言字符串

        constchar*cName =property_getName(property);

        // 转换为Objective C 字符串

        NSString *name = [NSString stringWithCString:cName encoding:NSUTF8StringEncoding];

        [propertiesArrayaddObject:name];

    }

    free(properties);

    NSLog(@"propertiesArray == %@",propertiesArray);

    //获取成员变量列表

    NSMutableArray *ivarArray = [NSMutableArray array];

    Ivar *ivarList = class_copyIvarList([UIAlertAction class], &count);

    for(inti =0; i < count; i++) {

        IvarmyIvar = ivarList[i];

        constchar*ivarName =ivar_getName(myIvar);

        [ivarArrayaddObject:[NSString stringWithUTF8String:ivarName]];

    }

    free(ivarList);

    NSLog(@"ivarArray == %@",ivarArray);

}

8.3以下版本打印结果:

高版本:

通过以上内容可见:成员变量在不同版本下不一定存在,当成员变量存在时我们才能使用KVC进行修改,否则系统会奔溃。

谨记 KVC 要慎用!!!

上一篇下一篇

猜你喜欢

热点阅读