YYModel icon indicating copy to clipboard operation
YYModel copied to clipboard

YYClassPropertyInfo中按位或运算符

Open BlankConan opened this issue 5 years ago • 1 comments

我想知道 YYClassPropertyInfo 在初始化的时候,对 type encoding 为什么使用 按位或的操作,为什么不直接对 type 赋值,这样做有什么好处吗?

          YYEncodingType type = 0;
            case 'R': {
                type |= YYEncodingTypePropertyReadonly;
            } break;
            case 'C': {
                type |= YYEncodingTypePropertyCopy;
            } break;
            case '&': {
                type |= YYEncodingTypePropertyRetain;
            } break;
            case 'N': {
                type |= YYEncodingTypePropertyNonatomic;
            } break;
            case 'D': {
                type |= YYEncodingTypePropertyDynamic;
            } break;
            case 'W': {
                type |= YYEncodingTypePropertyWeak;
            } break;
            case 'G': {
                type |= YYEncodingTypePropertyCustomGetter;
                if (attrs[i].value) {
                    _getter = NSSelectorFromString([NSString stringWithUTF8String:attrs[i].value]);
                }
            } break;

以及在 _YYModelPropertyMeta 的构造方法中:

    if ((meta->_type & YYEncodingTypeMask) == YYEncodingTypeObject) {
        meta->_nsType = YYClassGetNSType(propertyInfo.cls);
    }

希望大神们能解释一下,感谢!!!

BlankConan avatar Jan 14 '20 07:01 BlankConan

这个我认为有两个优点 1.内存上讲:这种方式可以使用一个int型的变量type,表示变量类型、修饰符、方法参数类型,分别使用8个二进制位表示,它在一定程度上节省了内存使用 2. 功能上讲:一个int型变量type,将某个变量的类型、修饰符、表方法参数时的修饰类型聚合在一起,体现了代码聚合性

以上两点为个人拙见,不当之处烦请提示,当然这也可能只是作者的一种编码习惯或者说喜好

| | 曾维俊 | | 邮箱:[email protected] |

签名由 网易邮箱大师 定制

在2020年01月14日 15:21,Conan 写道:

我想知道 YYClassPropertyInfo 在初始化的时候,对 type encoding 为什么使用 按位或的操作,为什么不直接对 type 赋值,这样做有什么好处吗?

      YYEncodingType type = 0;

        case 'R': {

            type |= YYEncodingTypePropertyReadonly;

        } break;

        case 'C': {

            type |= YYEncodingTypePropertyCopy;

        } break;

        case '&': {

            type |= YYEncodingTypePropertyRetain;

        } break;

        case 'N': {

            type |= YYEncodingTypePropertyNonatomic;

        } break;

        case 'D': {

            type |= YYEncodingTypePropertyDynamic;

        } break;

        case 'W': {

            type |= YYEncodingTypePropertyWeak;

        } break;

        case 'G': {

            type |= YYEncodingTypePropertyCustomGetter;

            if (attrs[i].value) {

                _getter = NSSelectorFromString([NSString stringWithUTF8String:attrs[i].value]);

            }

        } break;

希望大神们能解释一下,感谢!!!

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or unsubscribe.

zengweijun avatar Jan 14 '20 07:01 zengweijun