Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6028389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:43:58+00:00 2026-05-23T04:43:58+00:00

In Objective-C, it’s common to override -description with a method that prints the object

  • 0

In Objective-C, it’s common to override -description with a method that prints the object ID and instance variable names/values. I’d like to make a generic -description method that does this through introspection, rather than manually doing this for each class. I’d like the output to be something like:

<ClassName: 0x??????, ivar1: value1, ivar2: value2, ivar3: value3, ...>

It would also be nice to sort by instance variable name (so they are always in the same order). Finally, if this could be put into a category safely overriding the NSObject functionality, that would be perfect (but I see that’s not straightforward, as NSObject.m has a warning about using -[NSString stringWithFormat:] in -description).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T04:43:58+00:00Added an answer on May 23, 2026 at 4:43 am

    This is a great question! Because I haven’t found anything like this, I wrote a small function that does this for you. It replaces - (NSString *)description of NSObject using method swizzling (yes, I’m this evil. MAHAHAHAHAHahahahaha) and prints the ivar in the order they also appear in the class (you can edit it easily to display them in alphabetical order).

    DON’T! forget to call NSObjectSwizzleDescription()!

    .h file:

    @interface NSObject (JSObjectAdditions)
    @end
    
    
    void NSObjectSwizzleDescription();
    

    .m file:

    #import <objc/objc.h>
    #import "JSObject.h"
    
    @implementation NSObject (JSObjectAdditions)
    
    - (NSString *)verboseDescription
    {
        NSMutableString *description = [NSMutableString stringWithFormat:@"<%@: %p>", NSStringFromClass([self class]), self];
    
        uint32_t ivarCount;
        Ivar *ivars = class_copyIvarList([self class], &ivarCount);
    
        if(ivars)
        {
            [description appendString:@"\n{"];
    
            for(uint32_t i=0; i<ivarCount; i++)
            {
                Ivar ivar = ivars[i];
                const char *ivarType = ivar_getTypeEncoding(ivar);
                id ivarObject = object_getIvar(self, ivar);
    
                [description appendFormat:@"\n   %s: ", ivar_getName(ivar)];
    
                // Default signed data types
                if(strcmp(ivarType, "c") == 0)
                {
                    char character = (char)ivarObject;
                    [description appendFormat:@"'%c'", character];
                }
                else if(strcmp(ivarType, "i") == 0 || strcmp(ivarType, "l") == 0) // l is also 32 bit in the 64 bit runtime environment
                {
                    int integer = (int)ivarObject;
                    [description appendFormat:@"%i", integer];
                }
                else if(strcmp(ivarType, "s") == 0)
                {
                    short shortVal = (short)ivarObject;
                    [description appendFormat:@"%i", (int)shortVal];
                }
                else if(strcmp(ivarType, "q") == 0)
                {
                    long long longVal = (long long)ivarObject;
                    [description appendFormat:@"%l", longVal];
                }
                // Default unsigned data types
                else if(strcmp(ivarType, "C") == 0)
                {
                    unsigned char chracter = (unsigned char)ivarObject;
                    [description appendFormat:@"'%c'", chracter];
                }
                else if(strcmp(ivarType, "I") == 0 || strcmp(ivarType, "L") == 0)
                {
                    unsigned int integer = (unsigned int)ivarObject;
                    [description appendFormat:@"%u", integer];
                }
                else if(strcmp(ivarType, "S") == 0)
                {
                    unsigned short shortVal = (unsigned short)ivarObject;
                    [description appendFormat:@"%i", (int)shortVal];
                }
                else if(strcmp(ivarType, "Q") == 0)
                {
                    unsigned long long longVal = (unsigned long long)ivarObject;
                    [description appendFormat:@"%ll", longVal];
                }
                // Floats'n'stuff
                else if(strcmp(ivarType, "f") == 0)
                {
                    float floatVal;
                    memcpy(&floatVal, &ivarObject, sizeof(float));
    
                    [description appendFormat:@"%f", floatVal];
                }
                else if(strcmp(ivarType, "d") == 0)
                {
                    double doubleVal;
                    memcpy(&doubleVal, &ivarObject, sizeof(double));
    
                    [description appendFormat:@"%f", doubleVal];
                }
                // Boolean and pointer
                else if(strcmp(ivarType, "B") == 0)
                {
                    BOOL booleanVal = (BOOL)ivarObject;
                    [description appendFormat:@"%@", (booleanVal ? @"YES" : @"NO")];
                }
                else if(strcmp(ivarType, "v") == 0)
                {
                    void *pointer = (void *)ivarObject;
                    [description appendFormat:@"%p", pointer];
                }
                else if(strcmp(ivarType, "*") == 0 || strcmp(ivarType, ":") == 0) // SEL is just a typecast for a cstring
                {
                    char *cstring = (char *)ivarObject;
                    [description appendFormat:@"\"%s\"", cstring];
                }
                else if(strncmp(ivarType, "@", 1) == 0)
                {
                    [description appendFormat:@"%@", ivarObject];
                }
                else if(strcmp(ivarType, "#") == 0)
                {
                    Class objcClass = (Class)ivarObject;
                    [description appendFormat:@"%s", class_getName(objcClass)];
                }
                else
                    [description appendString:@"???"];
            }
    
            [description appendString:@"\n}"];
            free(ivars);
        }
    
        return description; 
    }
    
    @end
    
    
    void NSObjectSwizzleDescription()
    {
        Method origMethod = class_getInstanceMethod([NSObject class], @selector(description));
        Method newMethod = class_getInstanceMethod([NSObject class], @selector(verboseDescription));
    
        method_exchangeImplementations(origMethod, newMethod);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Objective-C uses a sophisticated message-passing system when one object calls a method on another
In Objective-C, if I override a class method using a category, is there a
In Objective-C 2.0, I usually make an assign property for ivars that are primitive
Objective-C, or Cocoa specifically, supports variadic arguments, like for example class the method on
In Objective C on iOS, are instance variables of object reference type ( id
Objective: Be able to nest a resource, like records inside of users so that
Objective-C has no namespaces; it's much like C, everything is within one global namespace.
Objective : Make a progress bar where users can check how much of a
In Objective-C, what's the difference between declaring a variable id versus declaring it NSObject
Objective: I would like to be able to list the available COM Ports on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.