NSObject(QMUI_KeyValueCoding)

@interface NSObject (QMUI_KeyValueCoding)

/**
 iOS 13 下系统禁止通过 KVC 访问私有 API,因此提供这种方式在遇到 access prohibited 的异常时可以取代 valueForKey: 使用。
 
 对 iOS 12 及以下的版本,等价于 valueForKey:。
 
 @note QMUI 提供2种方式兼容系统的 access prohibited 异常:
 1. 通过将配置表的 IgnoreKVCAccessProhibited 置为 YES 来全局屏蔽系统的异常警告,代码中依然正常使用系统的 valueForKey:、setValue:forKey:,当开启后再遇到 access prohibited 异常时,将会用 QMUIWarnLog 来提醒,不再中断 App 的运行,这是首选推荐方案。
 2. 使用 qmui_valueForKey:、qmui_setValue:forKey: 代替系统的 valueForKey:、setValue:forKey:,适用于不希望全局屏蔽,只针对某个局部代码自己处理的场景。
 
 @link https://github.com/Tencent/QMUI_iOS/issues/617
 
 @param key ivar 属性名,支持下划线或不带下划线
 @return key 对应的 value,如果该 key 原本是非对象的值,会被用 NSNumber、NSValue 包裹后返回
 */
- (nullable id)qmui_valueForKey:(NSString *)key;

/**
 iOS 13 下系统禁止通过 KVC 访问私有 API,因此提供这种方式在遇到 access prohibited 的异常时可以取代 setValue:forKey: 使用。
 
 对 iOS 12 及以下的版本,等价于 setValue:forKey:。
 
 @note QMUI 提供2种方式兼容系统的 access prohibited 异常:
 1. 通过将配置表的 IgnoreKVCAccessProhibited 置为 YES 来全局屏蔽系统的异常警告,代码中依然正常使用系统的 valueForKey:、setValue:forKey:,当开启后再遇到 access prohibited 异常时,将会用 QMUIWarnLog 来提醒,不再中断 App 的运行,这是首选推荐方案。
 2. 使用 qmui_valueForKey:、qmui_setValue:forKey: 代替系统的 valueForKey:、setValue:forKey:,适用于不希望全局屏蔽,只针对某个局部代码自己处理的场景。
 
 @link https://github.com/Tencent/QMUI_iOS/issues/617
 
 @param key ivar 属性名,支持下划线或不带下划线
 @return key 对应的 value,如果该 key 原本是非对象的值,会被用 NSNumber、NSValue 包裹后返回
 */
- (void)qmui_setValue:(nullable id)value forKey:(NSString *)key;

/**
 检查给定的 key 是否可以用于当前对象的 valueForKey: 调用。
 
 @note 这是针对 valueForKey: 内部查找 key 的逻辑的精简版,去掉了一些不常用的,如果按精简版查找不到,会返回 NO(但按完整版可能是能查找到的),避免抛出异常。文档描述的查找方法完整版请查看 https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/KeyValueCoding/SearchImplementation.html
 */
- (BOOL)qmui_canGetValueForKey:(NSString *)key;

/**
检查给定的 key 是否可以用于当前对象的 setValue:forKey: 调用。

@note 对于 setter 而言这就是完整版的检查流程,可核对文档 https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/KeyValueCoding/SearchImplementation.html
*/
- (BOOL)qmui_canSetValueForKey:(NSString *)key;

@end

Undocumented

  • iOS 13 下系统禁止通过 KVC 访问私有 API,因此提供这种方式在遇到 access prohibited 的异常时可以取代 valueForKey: 使用。

    对 iOS 12 及以下的版本,等价于 valueForKey:。

    Note

    QMUI 提供2种方式兼容系统的 access prohibited 异常:
    • 通过将配置表的 IgnoreKVCAccessProhibited 置为 YES 来全局屏蔽系统的异常警告,代码中依然正常使用系统的 valueForKey:、setValue:forKey:,当开启后再遇到 access prohibited 异常时,将会用 QMUIWarnLog 来提醒,不再中断 App 的运行,这是首选推荐方案。
    • 使用 qmui_valueForKey:、qmui_setValue:forKey: 代替系统的 valueForKey:、setValue:forKey:,适用于不希望全局屏蔽,只针对某个局部代码自己处理的场景。

    @link https://github.com/Tencent/QMUI_iOS/issues/617

    Declaration

    Objective-C

    - (nullable id)qmui_valueForKey:(nonnull NSString *)key;

    Swift

    func qmui_value(forKey key: String) -> Any?

    Return Value

    key 对应的 value,如果该 key 原本是非对象的值,会被用 NSNumber、NSValue 包裹后返回

  • iOS 13 下系统禁止通过 KVC 访问私有 API,因此提供这种方式在遇到 access prohibited 的异常时可以取代 setValue:forKey: 使用。

    对 iOS 12 及以下的版本,等价于 setValue:forKey:。

    Note

    QMUI 提供2种方式兼容系统的 access prohibited 异常:
    • 通过将配置表的 IgnoreKVCAccessProhibited 置为 YES 来全局屏蔽系统的异常警告,代码中依然正常使用系统的 valueForKey:、setValue:forKey:,当开启后再遇到 access prohibited 异常时,将会用 QMUIWarnLog 来提醒,不再中断 App 的运行,这是首选推荐方案。
    • 使用 qmui_valueForKey:、qmui_setValue:forKey: 代替系统的 valueForKey:、setValue:forKey:,适用于不希望全局屏蔽,只针对某个局部代码自己处理的场景。

    @link https://github.com/Tencent/QMUI_iOS/issues/617

    Declaration

    Objective-C

    - (void)qmui_setValue:(nullable id)value forKey:(nonnull NSString *)key;

    Swift

    func qmui_setValue(_ value: Any?, forKey key: String)

    Return Value

    key 对应的 value,如果该 key 原本是非对象的值,会被用 NSNumber、NSValue 包裹后返回

  • 检查给定的 key 是否可以用于当前对象的 valueForKey: 调用。

    Note

    这是针对 valueForKey: 内部查找 key 的逻辑的精简版,去掉了一些不常用的,如果按精简版查找不到,会返回 NO(但按完整版可能是能查找到的),避免抛出异常。文档描述的查找方法完整版请查看 https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/KeyValueCoding/SearchImplementation.html

    Declaration

    Objective-C

    - (BOOL)qmui_canGetValueForKey:(nonnull NSString *)key;

    Swift

    func qmui_canGetValue(forKey key: String) -> Bool
  • 检查给定的 key 是否可以用于当前对象的 setValue:forKey: 调用。

    Note

    对于 setter 而言这就是完整版的检查流程,可核对文档 https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/KeyValueCoding/SearchImplementation.html

    Declaration

    Objective-C

    - (BOOL)qmui_canSetValueForKey:(nonnull NSString *)key;

    Swift

    func qmui_canSetValue(forKey key: String) -> Bool