Search code examples
objective-ccocoastructnslog

Printing a struct using description


I would like to know if it is possible to use the description function in the Cocoa framework to log the contents of a struct. For example:

    typedef struct {float a,b,c;}list;
    list testlist = {1.0,2.5,3.9};
    NSLog(@"%@",testlist); //--> 1.0,2.5,3.9

Solution

  • No. The description message is a method found in the NSObject protocol, so by definition, must be an object. There is, however, a more convenient way of log debugging, using a LOG_EXPR() macro. This will take objects and structs:

    LOG_EXPR(testlist);

    Which would output:

    testlist = {1.0, 2.5, 3.9};

    This code can be found here.