runtime中property的赋值问题

发布于 2022-09-04 11:57:15 字数 914 浏览 10 评论 0

unsigned int count;

//在运行时创建继承自NSObject的People类
Class People = objc_allocateClassPair([NSObject class], "People", 0);

//完成People类的创建
objc_registerClassPair(People);

objc_property_attribute_t type = {"T", "@\"NSString\""};
objc_property_attribute_t attribute2 = {"N",""};//value无意义时通常设置为空
objc_property_attribute_t ownership = { "C", "" };
objc_property_attribute_t backingivar = { "V", "_pro"};
objc_property_attribute_t attrs[] = {type,attribute2, ownership, backingivar};

//向People类中添加名为pro的属性,属性的4个特性包含在attributes中
BOOL y = class_addProperty(People, "pro", attrs, 4);
NSLog(@"%d",y);

//创建People对象p1
id p1 = [[People alloc]init];

objc_property_t * properties = class_copyPropertyList(People, &count);
for (int i = 0; i<count; i++) {
    NSLog(@"属性的名称为 : %s",property_getName(properties[i]));
    NSLog(@"属性的特性字符串为: %s",property_getAttributes(properties[i]));
}

//请问怎么为pro赋值?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

酸甜透明夹心 2022-09-11 11:57:15
- (void)dynamic2{
    
    unsigned int count;
    
    //在运行时创建继承自NSObject的People类
    Class People = objc_allocateClassPair([NSObject class], "People", 0);
    
    NSString *proName = @"pro";
    
    class_addIvar(People, [proName cStringUsingEncoding:NSUTF8StringEncoding], sizeof(NSString *), log2(sizeof(NSString*)), @encode(NSString*));

    //完成People类的创建
    objc_registerClassPair(People);
    objc_property_attribute_t type = {"T", "@\"NSString\""};
    objc_property_attribute_t attribute2 = {"N",""};//value无意义时通常设置为空
    objc_property_attribute_t ownership = { "C", "" };
    objc_property_attribute_t backingivar = { "V", [proName cStringUsingEncoding:NSUTF8StringEncoding]};
    objc_property_attribute_t attrs[] = {type,attribute2, ownership, backingivar};
    
    //向People类中添加名为pro的属性,属性的4个特性包含在attributes中
    if (class_addProperty(People, [proName cStringUsingEncoding:NSUTF8StringEncoding], attrs, 4)) {
        
        NSString *s = [NSString stringWithFormat:@"set%@:",[proName capitalizedString]];
        
        //添加get和set方法
        class_addMethod([People class], NSSelectorFromString(proName), (IMP)getter, "@@:");
        class_addMethod([People class], NSSelectorFromString(s), (IMP)setter, "v@:@");
        
    }
    
    //创建People对象p1
    id p1 = [[People alloc]init];
    
    objc_property_t * properties = class_copyPropertyList(People, &count);
    for (int i = 0; i<count; i++) {
        NSLog(@"属性的名称为 : %s",property_getName(properties[i]));
        NSLog(@"属性的特性字符串为: %s",property_getAttributes(properties[i]));
    }
    
    [p1 setValue:@"111" forKey:@"pro"];
    NSLog(@"%@", [p1 valueForKey:@"pro"]);
}

id getter(id self1, SEL _cmd1) {
    NSString *key = NSStringFromSelector(_cmd1);
    Ivar ivar = class_getInstanceVariable([self1 class], [key cStringUsingEncoding:NSUTF8StringEncoding]);  
    NSString *s = object_getIvar(self1, ivar);
    return s;
}

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], [key cStringUsingEncoding:NSUTF8StringEncoding]);  
    object_setIvar(self1, ivar, newValue);
盗琴音 2022-09-11 11:57:15

按照你添加的方法, 这个也是可以的

    SEL setter = NSSelectorFromString(@"setPro:");
    SEL getter = NSSelectorFromString(@"pro");
    if ([p1 respondsToSelector:setter] && [p1 respondsToSelector:getter]) {
        [p1 performSelector:setter withObject:@"NBA"];
        id d = [p1  performSelector:getter withObject:nil];
        NSLog(@"d,%@",d);
    }
‖放下 2022-09-11 11:57:15

都是大神啊,求教这些语法我从来没学过,我常见的是什么Objective-C 最常用的那种语法。
请问这些语法是属于什么知识呢?在什么地方常用呢?多谢大大解答啊~ 我要掌握这些看着很酷的语法~~~~

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文