ios中给分类添加属性

ios中给分类添加属性,第1张

ios中利用类别给已有的类扩展方法是可以的,但是如果直接的添加属性是会报错的。利用runtime可以达到添加属性的目的。

1.先创建一个分类,以下以UIImage为例子。

2.增加需要的属性。

3.导入runtime框架,重写set方法和get方法。

//其中注意以下的参数是用来表示创建的属性的类型的

1. catagory本来就不能为原有类添加属性, 只能添加方法..

2. 但是可以通过runtime.h运行时来为分类绑定属性.

3. 正常写上属性, 然后为属性绑定set / get方法.

在ios运行过程中,有几种方式能够动态的添加属性。

1-通过runtime动态关联对象

主要用到了objc_setAssociatedObject,objc_getAssociatedObject以及objc_removeAssociatedObjects

//在目标target上添加关联对象,属性名propertyname(也能用来添加block),值value

+ (void)addAssociatedWithtarget:(id)target withPropertyName:(NSString *)propertyName withValue:(id)value {

id property = objc_getAssociatedObject(target, &propertyName)

if(property == nil)

{

property = value

objc_setAssociatedObject(target, &propertyName, property, OBJC_ASSOCIATION_RETAIN)

}

}

//获取目标target的指定关联对象值

+ (id)getAssociatedValueWithTarget:(id)target withPropertyName:(NSString *)propertyName {

id property = objc_getAssociatedObject(target, &propertyName)

return property

}

优点:这种方式能够使我们快速的在一个已有的class内部添加一个动态属性或block块。

缺点:不能像遍历属性一样的遍历我们所有关联对象,且不能移除制定的关联对象,只能通过removeAssociatedObjects方法移除所有关联对象。

2-通过runtime动态添加Ivar

主要用到objc_allocateClassPair,class_addIvar,objc_registerClassPair

//在目标target上添加属性(已经存在的类不支持,可跳进去看注释),属性名propertyname,值value

+ (void)addIvarWithtarget:(id)target withPropertyName:(NSString *)propertyName withValue:(id)value {

if (class_addIvar([target class], [propertyName UTF8String], sizeof(id), log2(sizeof(id)), "@")) {

YYLog(@"创建属性Ivar成功")

}

}

//获取目标target的指定属性值

+ (id)getIvarValueWithTarget:(id)target withPropertyName:(NSString *)propertyName {

Ivar ivar = class_getInstanceVariable([target class], [propertyName UTF8String])

if (ivar) {

id value = object_getIvar(target, ivar)

return value

} else {

return nil

}

}

优点:动态添加Ivar我们能够通过遍历Ivar得到我们所添加的属性。

缺点:不能在已存在的class中添加Ivar,所有说必须通过objc_allocateClassPair动态创建一个class,才能调用class_addIvar创建Ivar,最后通过objc_registerClassPair注册class。

3-通过runtime动态添加property

主要用到class_addProperty,class_addMethod,class_replaceProperty,class_getInstanceVariable

//在目标target上添加属性,属性名propertyname,值value

+ (void)addPropertyWithtarget:(id)target withPropertyName:(NSString *)propertyName withValue:(id)value {

//先判断有没有这个属性,没有就添加,有就直接赋值

Ivar ivar = class_getInstanceVariable([target class], [[NSString stringWithFormat:@"_%@", propertyName] UTF8String])

if (ivar) {

return

}

/*

objc_property_attribute_t type = { "T", "@\"NSString\"" }

objc_property_attribute_t ownership = { "C", "" }// C = copy

objc_property_attribute_t backingivar = { "V", "_privateName" }

objc_property_attribute_t attrs[] = { type, ownership, backingivar }

class_addProperty([SomeClass class], "name", attrs, 3)

*/

//objc_property_attribute_t所代表的意思可以调用getPropertyNameList打印,大概就能猜出

objc_property_attribute_t type = { "T", [[NSString stringWithFormat:@"@\"%@\"",NSStringFromClass([value class])] UTF8String] }

objc_property_attribute_t ownership = { "&", "N" }

objc_property_attribute_t backingivar = { "V", [[NSString stringWithFormat:@"_%@", propertyName] UTF8String] }

objc_property_attribute_t attrs[] = { type, ownership, backingivar }

if (class_addProperty([target class], [propertyName UTF8String], attrs, 3)) {

//添加get和set方法

class_addMethod([target class], NSSelectorFromString(propertyName), (IMP)getter, "@@:")

class_addMethod([target class], NSSelectorFromString([NSString stringWithFormat:@"set%@:",[propertyName capitalizedString]]), (IMP)setter, "v@:@")

//赋值

[target setValue:value forKey:propertyName]

NSLog(@"%@", [target valueForKey:propertyName])

YYLog(@"创建属性Property成功")

} else {

class_replaceProperty([target class], [propertyName UTF8String], attrs, 3)

//添加get和set方法

class_addMethod([target class], NSSelectorFromString(propertyName), (IMP)getter, "@@:")

class_addMethod([target class], NSSelectorFromString([NSString stringWithFormat:@"set%@:",[propertyName capitalizedString]]), (IMP)setter, "v@:@")

//赋值

[target setValue:value forKey:propertyName]

}

}

id getter(id self1, SEL _cmd1) {

NSString *key = NSStringFromSelector(_cmd1)

Ivar ivar = class_getInstanceVariable([self1 class], "_dictCustomerProperty") //basicsViewController里面有个_dictCustomerProperty属性

NSMutableDictionary *dictCustomerProperty = object_getIvar(self1, ivar)

return [dictCustomerProperty objectForKey:key]

}

void setter(id self1, SEL _cmd1, id newValue) {

//移除set

NSString *key = [NSStringFromSelector(_cmd1) stringByReplacingCharactersInRange:NSMakeRange(0, 3) withString:@""]

//首字母小写

NSString *head = [key substringWithRange:NSMakeRange(0, 1)]

head = [head lowercaseString]

key = [key stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:head]

//移除后缀 ":"

key = [key stringByReplacingCharactersInRange:NSMakeRange(key.length - 1, 1) withString:@""]

Ivar ivar = class_getInstanceVariable([self1 class], "_dictCustomerProperty") //basicsViewController里面有个_dictCustomerProperty属性

NSMutableDictionary *dictCustomerProperty = object_getIvar(self1, ivar)

if (!dictCustomerProperty) {

dictCustomerProperty = [NSMutableDictionary dictionary]

object_setIvar(self1, ivar, dictCustomerProperty)

}

[dictCustomerProperty setObject:newValue forKey:key]

}

+ (id)getPropertyValueWithTarget:(id)target withPropertyName:(NSString *)propertyName {

//先判断有没有这个属性,没有就添加,有就直接赋值

Ivar ivar = class_getInstanceVariable([target class], [[NSString stringWithFormat:@"_%@", propertyName] UTF8String])

if (ivar) {

return object_getIvar(target, ivar)

}

ivar = class_getInstanceVariable([target class], "_dictCustomerProperty") //basicsViewController里面有个_dictCustomerProperty属性

NSMutableDictionary *dict = object_getIvar(target, ivar)

if (dict &&[dict objectForKey:propertyName]) {

return [dict objectForKey:propertyName]

} else {

return nil

}

}

优点:这种方法能够在已有的类中添加property,且能够遍历到动态添加的属性。

缺点:比较麻烦,getter和setter需要自己写,且值也需要自己存储,如上面的代码,我是把setter中的值存储到了_dictCustomerProperty里面,在getter中再从_dictCustomerProperty读出值。

4-通过setValue:forUndefinedKey动态添加键值

这种方法优点类似property,需要重写setValue:forUndefinedKey和valueForUndefinedKey:,存值方式也一样,需要借助一个其它对象。由于这种方式没通过runtime,所以也比较容易理解。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/bake/11901013.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存