美文网首页
iOS Runtime 使用

iOS Runtime 使用

作者: a7cdfadad9f2 | 来源:发表于2017-11-14 11:20 被阅读0次

    使用runtime 首先需要导入 #import<objc/ runtime.h>

    runtime拷贝

    objc_setAssociatedObject(button, @"arr", array[i][@"childmenu"], OBJC_ASSOCIATION_COPY);

    这段代码告诉我们是以button对象为基础  key是arr。value是array[i][@"childmenu"]

    所以

    取值

    NSArray *arr = objc_getAssociatedObject(button, @"arr");

    就是这样。

    runtime 扩展  

    例如百度地图 标注 我们需要添加一个参数

    众所周知 BMKShape 是标注 属性类 里面有 title subtitle 俩个参数,我们不够用怎么办,需要这个时候需要创建以个分类Category;

    BMKShape+MapAnnotation.h

    @property (nonatomic,copy) NSString *addres;

    BMKShape+MapAnnotation.m

    char* const newdituaddres = "addres"; //声明一个char *类型的指针变量,指针指向了“addres”这个字符串

    -(void)setAddres:(NSString *)addres{

    objc_setAssociatedObject(self,newdituaddres,addres,OBJC_ASSOCIATION_RETAIN_NONATOMIC);

    }

    -(NSString *)addres{

    return objc_getAssociatedObject(self,newdituaddres);

    }

    相关文章

      网友评论

          本文标题:iOS Runtime 使用

          本文链接:https://www.haomeiwen.com/subject/oiuxvxtx.html