<small id='JFfaV'></small><noframes id='JFfaV'>

    <i id='JFfaV'><tr id='JFfaV'><dt id='JFfaV'><q id='JFfaV'><span id='JFfaV'><b id='JFfaV'><form id='JFfaV'><ins id='JFfaV'></ins><ul id='JFfaV'></ul><sub id='JFfaV'></sub></form><legend id='JFfaV'></legend><bdo id='JFfaV'><pre id='JFfaV'><center id='JFfaV'></center></pre></bdo></b><th id='JFfaV'></th></span></q></dt></tr></i><div id='JFfaV'><tfoot id='JFfaV'></tfoot><dl id='JFfaV'><fieldset id='JFfaV'></fieldset></dl></div>
    <tfoot id='JFfaV'></tfoot>
      <legend id='JFfaV'><style id='JFfaV'><dir id='JFfaV'><q id='JFfaV'></q></dir></style></legend>

        • <bdo id='JFfaV'></bdo><ul id='JFfaV'></ul>

        自动将属性值从一个对象复制到另一个类型不同但协议相同的对象 (Objective-C)

        时间:2024-04-15
      1. <tfoot id='MGM6V'></tfoot>
          <bdo id='MGM6V'></bdo><ul id='MGM6V'></ul>
        • <i id='MGM6V'><tr id='MGM6V'><dt id='MGM6V'><q id='MGM6V'><span id='MGM6V'><b id='MGM6V'><form id='MGM6V'><ins id='MGM6V'></ins><ul id='MGM6V'></ul><sub id='MGM6V'></sub></form><legend id='MGM6V'></legend><bdo id='MGM6V'><pre id='MGM6V'><center id='MGM6V'></center></pre></bdo></b><th id='MGM6V'></th></span></q></dt></tr></i><div id='MGM6V'><tfoot id='MGM6V'></tfoot><dl id='MGM6V'><fieldset id='MGM6V'></fieldset></dl></div>
        • <legend id='MGM6V'><style id='MGM6V'><dir id='MGM6V'><q id='MGM6V'></q></dir></style></legend>

                <tbody id='MGM6V'></tbody>
            1. <small id='MGM6V'></small><noframes id='MGM6V'>

                  本文介绍了自动将属性值从一个对象复制到另一个类型不同但协议相同的对象 (Objective-C)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有两个具有相同属性集的类,在协议中使用@property 指令声明,它们都实现了.现在我想知道是否可以使用第二类实例的值自动填充第一类的实例(反之亦然).我希望这种方法是健壮的,这样如果我更改协议中声明的属性,就不需要在复制方法中添加额外的代码.

                  I have two classes with the same set of properties, declared using the @property directive in a protocol, they both implement. Now I was wondering if it is possible to automatically populate an instance of the first class with the values from an instance of the second class (and vice-versa). I would like this approach to be robust, so that if I change the of properties declared in the protocol there will be no need to add extra code in the copying methods.

                  推荐答案

                  是的,考虑到确切的上下文,可能有多种方法可以解决此问题.

                  Yes, given the exact context there could be various approaches to this problem.

                  目前我能想到的一个方法是先获取源对象的所有属性,然后使用 setValue:value forKey:key 设置目标对象的值.

                  One I can think of at the moment is to first get all the properties of source object then use setValue:value forKey:key to set the values on the target object.

                  检索所有自定义属性的代码:

                  Code to retrieve all custom properties:

                  -(NSSet *)propertyNames {
                    NSMutableSet *propNames = [NSMutableSet set];
                    unsigned int outCount, i;
                    objc_property_t *properties = class_copyPropertyList([self class], &outCount);
                    for (i = 0; i < outCount; i++) {
                      objc_property_t property = properties[i];
                      NSString *propertyName = [[[NSString alloc] 
                        initWithCString:property_getName(property)] autorelease];
                      [propNames addObject:propertyName];
                    }
                    free(properties);
                  
                    return propNames;
                  }
                  

                  您可能需要查看 键-值编码编程指南了解更多信息.

                  You may want to checkout the Key-Value Coding Programming Guide for more information.

                  这篇关于自动将属性值从一个对象复制到另一个类型不同但协议相同的对象 (Objective-C)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Android如何在android中检测Edittext的Copy事件 下一篇:安卓文件拷贝

                  相关文章

                  • <bdo id='mF1Tb'></bdo><ul id='mF1Tb'></ul>

                  1. <tfoot id='mF1Tb'></tfoot>

                    <legend id='mF1Tb'><style id='mF1Tb'><dir id='mF1Tb'><q id='mF1Tb'></q></dir></style></legend>

                    <small id='mF1Tb'></small><noframes id='mF1Tb'>

                      <i id='mF1Tb'><tr id='mF1Tb'><dt id='mF1Tb'><q id='mF1Tb'><span id='mF1Tb'><b id='mF1Tb'><form id='mF1Tb'><ins id='mF1Tb'></ins><ul id='mF1Tb'></ul><sub id='mF1Tb'></sub></form><legend id='mF1Tb'></legend><bdo id='mF1Tb'><pre id='mF1Tb'><center id='mF1Tb'></center></pre></bdo></b><th id='mF1Tb'></th></span></q></dt></tr></i><div id='mF1Tb'><tfoot id='mF1Tb'></tfoot><dl id='mF1Tb'><fieldset id='mF1Tb'></fieldset></dl></div>