处理object_getIvar(id object, Ivar ivar)的返回值

发布于 2024-07-29 18:59:02 字数 263 浏览 8 评论 0原文

1) object_getIvar(id object, Ivar ivar) 如果 Ivar 是一个对象,则返回一个“id”,例如。 如果变量是 NSString,则可能包含该值的 id = NSString。 那是对的吗? 或者我需要做什么才能获取 Ivar 的价值。

2)如果 Ivar 是 float/int 等,将返回什么以及如何将其转换为我可以使用的东西(float 或 int 都可以,因为我可以使用 NSNumber numberWithXXX 将其转换为对象)。

1) object_getIvar(id object, Ivar ivar) returns an 'id' if the Ivar is an object eg. if the variabe is an NSString, presumably the id = NSString which contains the value. Is that correct? Or what do I need to do to access the value of the Ivar.

2) if the Ivar is a float/int etc. what will get returned and how do I convert it into something I can use (a float or int is fine as I can use NSNumber numberWithXXX to convert it to an object).

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

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

发布评论

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

评论(2

蝶舞 2024-08-05 18:59:02

1)正确。 正如所述文档

返回值
ivar 指定的实例变量的值,如果 object 为 nil,则为 nil。

2) 无论 Ivar 的类型如何,您都将始终获得 Ivar 所持有的值。 您可以使用 ivar_getTypeEncoding此处提供了各种类型编码的列表。

有了这些信息,您应该能够编写一个适当处理每种情况的开关; 例如:(

警告:前面未经测试的代码)

const char* typeEncoding = ivar_getTypeEncoding(var);

switch (typeEncoding) {
    case '@': {
        // handle class case
    } break;

    case 'i': {
        // handle int case
    } break;

    case 'f': {
        // handle float case
    } break;

    // .. and so on
}

1) Correct. As stated by the docs:

Return Value
The value of the instance variable specified by ivar, or nil if object is nil.

2) No matter the type of the Ivar you will always get the value that the Ivar holds. You can determine the type of the Ivar by using ivar_getTypeEncoding. There's a list of the various type encodings here.

With that information at hand, you should be able to write a switch that handles each case appropriately; e.g.:

(warning: non-tested code ahead)

const char* typeEncoding = ivar_getTypeEncoding(var);

switch (typeEncoding) {
    case '@': {
        // handle class case
    } break;

    case 'i': {
        // handle int case
    } break;

    case 'f': {
        // handle float case
    } break;

    // .. and so on
}
一绘本一梦想 2024-08-05 18:59:02

用于打印任意对象的描述的示例:

- (NSString *) qCustomDescription
{
    static int depth = 0;

    NSMutableString *resultString = [NSMutableString stringWithFormat: @"<%@: %p>", NSStringFromClass([self class]), self];

    uint32_t ivarCount;
    Ivar *ivars = class_copyIvarList([self class], &ivarCount);

    if( ivars )
    {
        ++depth;
        [resultString appendString: @"\n"];

        for( int tabs = depth; --tabs > 0; )
            [resultString appendString: @"\t"];

        [resultString appendString: @"{"];

        for( uint32_t i = 0; i < ivarCount; ++i )
        {
            Ivar ivar = ivars[i];
            const char* type = ivar_getTypeEncoding(ivar);
            const char* ivarName = ivar_getName( ivar );
            NSString* valueDescription = @"";
            NSString* name = [NSString stringWithCString: ivarName encoding: NSASCIIStringEncoding];

            switch( type[0] )
            {
                case '@':
                {
                    id v = object_getIvar(self, ivar);
                    if( v )
                    {
                        if( [self respondsToSelector: @selector(qDescriptionForValue:)] )
                            valueDescription = [self performSelector: @selector(qDescriptionForValue:) withObject: v];
                        else
                            valueDescription = [v description];
                    }
                    break;
                }

                case 'c':
                {
                    char v = ((char (*)(id, Ivar))object_getIvar)(self, ivar);
                    valueDescription = [NSString stringWithFormat: @"%c", v];
                    break;
                }

                case 'i':
                {
                    int v = ((int (*)(id, Ivar))object_getIvar)(self, ivar);
                    valueDescription = [NSString stringWithFormat: @"%i", v];
                    break;
                }

                case 's':
                {
                    short v = ((short (*)(id, Ivar))object_getIvar)(self, ivar);
                    valueDescription = [NSString stringWithFormat: @"%d", v];
                    break;
                }

                case 'l':
                {
                    long v = ((long (*)(id, Ivar))object_getIvar)(self, ivar);
                    valueDescription = [NSString stringWithFormat: @"%ld", v];
                    break;
                }

                case 'q':
                {
                    long long v = ((long long (*)(id, Ivar))object_getIvar)(self, ivar);
                    valueDescription = [NSString stringWithFormat: @"%lld", v];
                    break;
                }

                case 'C':
                {
                    unsigned char v = ((unsigned char (*)(id, Ivar))object_getIvar)(self, ivar);
                    valueDescription = [NSString stringWithFormat: @"%uc", v];
                    break;
                }

                case 'I':
                {
                    unsigned int v = ((unsigned int (*)(id, Ivar))object_getIvar)(self, ivar);
                    valueDescription = [NSString stringWithFormat: @"%u", v];
                    break;
                }

                case 'S':
                {
                    unsigned short v = ((unsigned short (*)(id, Ivar))object_getIvar)(self, ivar);
                    valueDescription = [NSString stringWithFormat: @"%u", v];
                    break;
                }

                case 'L':
                {
                    unsigned long v = ((unsigned long (*)(id, Ivar))object_getIvar)(self, ivar);
                    valueDescription = [NSString stringWithFormat: @"%lu", v];
                    break;
                }

                case 'Q':
                {
                    unsigned long long v = ((unsigned long long (*)(id, Ivar))object_getIvar)(self, ivar);
                    valueDescription = [NSString stringWithFormat: @"%llu", v];
                    break;
                }

                case 'f':
                {
                    float v = ((float (*)(id, Ivar))object_getIvar)(self, ivar);
                    valueDescription = [NSString stringWithFormat: @"%f", v];
                    break;
                }

                case 'd':
                {
                    double v = ((double (*)(id, Ivar))object_getIvar)(self, ivar);
                    valueDescription = [NSString stringWithFormat: @"%f", v];
                    break;
                }

                case 'B':
                {
                    BOOL v = ((BOOL (*)(id, Ivar))object_getIvar)(self, ivar);
                    valueDescription = [NSString stringWithFormat: @"%@", v ? @"YES" : @"NO"];
                    break;
                }

                case '*':
                {
                    char *v = ((char* (*)(id, Ivar))object_getIvar)(self, ivar);
                    valueDescription = [NSString stringWithFormat: @"%s", v];
                    break;
                }

                case '#':
                {
                    id v = object_getIvar(self, ivar);
                    valueDescription = [NSString stringWithFormat: @"Class: %s", object_getClassName(v)];
                    break;
                }

                case ':':
                {
                    SEL v = ((SEL (*)(id, Ivar))object_getIvar)(self, ivar);
                    valueDescription = [NSString stringWithFormat: @"Selector: %s", sel_getName(v)];
                    break;
                }

                case '[':
                case '{':
                case '(':
                case 'b':
                case '^':
                {
                    valueDescription = [NSString stringWithFormat: @"%s", type];
                    break;
                }

                default:
                    valueDescription = [NSString stringWithFormat: @"UNKNOWN TYPE: %s", type];
                    break;
            }

            [resultString appendString: @"\n"];

            for( int tabs = depth; --tabs >= 0; )
                [resultString appendString: @"\t"];

            [resultString appendFormat: @"%@: %@", name, valueDescription];
        }

        [resultString appendString: @"\n"];

        for( int tabs = depth; --tabs > 0; )
            [resultString appendString: @"\t"];

        [resultString appendString: @"}"];
        --depth;

        free(ivars);
    }

    return resultString;
}

Example use for printing the description of arbitrary objects:

- (NSString *) qCustomDescription
{
    static int depth = 0;

    NSMutableString *resultString = [NSMutableString stringWithFormat: @"<%@: %p>", NSStringFromClass([self class]), self];

    uint32_t ivarCount;
    Ivar *ivars = class_copyIvarList([self class], &ivarCount);

    if( ivars )
    {
        ++depth;
        [resultString appendString: @"\n"];

        for( int tabs = depth; --tabs > 0; )
            [resultString appendString: @"\t"];

        [resultString appendString: @"{"];

        for( uint32_t i = 0; i < ivarCount; ++i )
        {
            Ivar ivar = ivars[i];
            const char* type = ivar_getTypeEncoding(ivar);
            const char* ivarName = ivar_getName( ivar );
            NSString* valueDescription = @"";
            NSString* name = [NSString stringWithCString: ivarName encoding: NSASCIIStringEncoding];

            switch( type[0] )
            {
                case '@':
                {
                    id v = object_getIvar(self, ivar);
                    if( v )
                    {
                        if( [self respondsToSelector: @selector(qDescriptionForValue:)] )
                            valueDescription = [self performSelector: @selector(qDescriptionForValue:) withObject: v];
                        else
                            valueDescription = [v description];
                    }
                    break;
                }

                case 'c':
                {
                    char v = ((char (*)(id, Ivar))object_getIvar)(self, ivar);
                    valueDescription = [NSString stringWithFormat: @"%c", v];
                    break;
                }

                case 'i':
                {
                    int v = ((int (*)(id, Ivar))object_getIvar)(self, ivar);
                    valueDescription = [NSString stringWithFormat: @"%i", v];
                    break;
                }

                case 's':
                {
                    short v = ((short (*)(id, Ivar))object_getIvar)(self, ivar);
                    valueDescription = [NSString stringWithFormat: @"%d", v];
                    break;
                }

                case 'l':
                {
                    long v = ((long (*)(id, Ivar))object_getIvar)(self, ivar);
                    valueDescription = [NSString stringWithFormat: @"%ld", v];
                    break;
                }

                case 'q':
                {
                    long long v = ((long long (*)(id, Ivar))object_getIvar)(self, ivar);
                    valueDescription = [NSString stringWithFormat: @"%lld", v];
                    break;
                }

                case 'C':
                {
                    unsigned char v = ((unsigned char (*)(id, Ivar))object_getIvar)(self, ivar);
                    valueDescription = [NSString stringWithFormat: @"%uc", v];
                    break;
                }

                case 'I':
                {
                    unsigned int v = ((unsigned int (*)(id, Ivar))object_getIvar)(self, ivar);
                    valueDescription = [NSString stringWithFormat: @"%u", v];
                    break;
                }

                case 'S':
                {
                    unsigned short v = ((unsigned short (*)(id, Ivar))object_getIvar)(self, ivar);
                    valueDescription = [NSString stringWithFormat: @"%u", v];
                    break;
                }

                case 'L':
                {
                    unsigned long v = ((unsigned long (*)(id, Ivar))object_getIvar)(self, ivar);
                    valueDescription = [NSString stringWithFormat: @"%lu", v];
                    break;
                }

                case 'Q':
                {
                    unsigned long long v = ((unsigned long long (*)(id, Ivar))object_getIvar)(self, ivar);
                    valueDescription = [NSString stringWithFormat: @"%llu", v];
                    break;
                }

                case 'f':
                {
                    float v = ((float (*)(id, Ivar))object_getIvar)(self, ivar);
                    valueDescription = [NSString stringWithFormat: @"%f", v];
                    break;
                }

                case 'd':
                {
                    double v = ((double (*)(id, Ivar))object_getIvar)(self, ivar);
                    valueDescription = [NSString stringWithFormat: @"%f", v];
                    break;
                }

                case 'B':
                {
                    BOOL v = ((BOOL (*)(id, Ivar))object_getIvar)(self, ivar);
                    valueDescription = [NSString stringWithFormat: @"%@", v ? @"YES" : @"NO"];
                    break;
                }

                case '*':
                {
                    char *v = ((char* (*)(id, Ivar))object_getIvar)(self, ivar);
                    valueDescription = [NSString stringWithFormat: @"%s", v];
                    break;
                }

                case '#':
                {
                    id v = object_getIvar(self, ivar);
                    valueDescription = [NSString stringWithFormat: @"Class: %s", object_getClassName(v)];
                    break;
                }

                case ':':
                {
                    SEL v = ((SEL (*)(id, Ivar))object_getIvar)(self, ivar);
                    valueDescription = [NSString stringWithFormat: @"Selector: %s", sel_getName(v)];
                    break;
                }

                case '[':
                case '{':
                case '(':
                case 'b':
                case '^':
                {
                    valueDescription = [NSString stringWithFormat: @"%s", type];
                    break;
                }

                default:
                    valueDescription = [NSString stringWithFormat: @"UNKNOWN TYPE: %s", type];
                    break;
            }

            [resultString appendString: @"\n"];

            for( int tabs = depth; --tabs >= 0; )
                [resultString appendString: @"\t"];

            [resultString appendFormat: @"%@: %@", name, valueDescription];
        }

        [resultString appendString: @"\n"];

        for( int tabs = depth; --tabs > 0; )
            [resultString appendString: @"\t"];

        [resultString appendString: @"}"];
        --depth;

        free(ivars);
    }

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