Latest 0.0.4
Homepage https://github.com/stanislaw/NSStringFromAnyObject
License MIT
Platforms ios 5.0, osx 10.7, requires ARC
Authors

NSStringFromAnyObject creation was caused by evolution of EchoLogger project which used to provide the same functionality: NSString representations for arbitrary objects (be it Objective-C or C objects) by means of Clang’s __attribute__((overloadable)).

NSStringFromAnyObject uses Objective-C Runtime introspection instead, as it appears to be a more consistent approach (though probably a bit slower). It is inspired by ATLog project written by Yan Rabovik.

The following types are currently supported:

  • Objective-C types
    • Object id aka %@
    • Class Class
    • Selector SEL
  • Foundation
    • NSRange
  • C types
    • void * and const void * pointers.
    • Numeric types: BOOL, double, float, int, long and others.
    • C strings: char * and const char *.
    • Typical structs like struct of two floats or four doubles (MKMapSize, MKMapRect and similar).
  • UIKit types: CGRect, CGSize, CGPoint.

NSStringFromAnyObject can be extended with additional "any object handlers": see how the handlers for objects of UIKit are written: they are decoupled from main C/Objective-C "any objects handler" so they can be included manually if these frameworks are needed. Browse the code to see how to write custom handlers.

Feedback is highly appreciated!

Build Status

Example

#import <Foundation/Foundation.h>
#import "UIKit.h"

#import "NSStringFromAnyObject.h"

int main(int argc, const char * argv[])
{
    @autoreleasepool {
        NSString *objectRepresentation;

        // Objective-C

        objectRepresentation = NSStringFromAnyObject( nil );
        printf("%sn", [objectRepresentation cStringUsingEncoding:NSUTF8StringEncoding]);

        objectRepresentation = NSStringFromAnyObject( @[ @(1) ] );
        printf("%sn", [objectRepresentation cStringUsingEncoding:NSUTF8StringEncoding]);

        objectRepresentation = NSStringFromAnyObject( @(1) );
        printf("%sn", [objectRepresentation cStringUsingEncoding:NSUTF8StringEncoding]);

        objectRepresentation = NSStringFromAnyObject( [@(1) class] );
        printf("%sn", [objectRepresentation cStringUsingEncoding:NSUTF8StringEncoding]);

        objectRepresentation = NSStringFromAnyObject( @selector(main) );
        printf("%sn", [objectRepresentation cStringUsingEncoding:NSUTF8StringEncoding]);

        // Foundation

        objectRepresentation = NSStringFromAnyObject( NSMakeRange(0, 10) );
        printf("%sn", [objectRepresentation cStringUsingEncoding:NSUTF8StringEncoding]);

        // C pointers

        objectRepresentation = NSStringFromAnyObject( NULL );
        printf("%sn", [objectRepresentation cStringUsingEncoding:NSUTF8StringEncoding]);

        void *ptr = &printf;
        objectRepresentation = NSStringFromAnyObject( ptr );
        printf("%sn", [objectRepresentation cStringUsingEncoding:NSUTF8StringEncoding]);

        const void *const_ptr = &printf;
        objectRepresentation = NSStringFromAnyObject( const_ptr );
        printf("%sn", [objectRepresentation cStringUsingEncoding:NSUTF8StringEncoding]);

        // C numbers

        objectRepresentation = NSStringFromAnyObject( (BOOL)YES );
        printf("%sn", [objectRepresentation cStringUsingEncoding:NSUTF8StringEncoding]);

        objectRepresentation = NSStringFromAnyObject( 1.24 );
        printf("%sn", [objectRepresentation cStringUsingEncoding:NSUTF8StringEncoding]);

        objectRepresentation = NSStringFromAnyObject( 1.24f );
        printf("%sn", [objectRepresentation cStringUsingEncoding:NSUTF8StringEncoding]);

        objectRepresentation = NSStringFromAnyObject( 1 );
        printf("%sn", [objectRepresentation cStringUsingEncoding:NSUTF8StringEncoding]);

        objectRepresentation = NSStringFromAnyObject( (short)1 );
        printf("%sn", [objectRepresentation cStringUsingEncoding:NSUTF8StringEncoding]);

        objectRepresentation = NSStringFromAnyObject( (long)1000L );
        printf("%sn", [objectRepresentation cStringUsingEncoding:NSUTF8StringEncoding]);

        objectRepresentation = NSStringFromAnyObject( (long long)100000000000LL );
        printf("%sn", [objectRepresentation cStringUsingEncoding:NSUTF8StringEncoding]);

        objectRepresentation = NSStringFromAnyObject( (char)'c' );
        printf("%sn", [objectRepresentation cStringUsingEncoding:NSUTF8StringEncoding]);

        objectRepresentation = NSStringFromAnyObject( (unsigned char)'c' );
        printf("%sn", [objectRepresentation cStringUsingEncoding:NSUTF8StringEncoding]);

        // C strings

        char *str = "A string";
        const char *conststr = (const char *)str;

        objectRepresentation = NSStringFromAnyObject( str );
        printf("%sn", [objectRepresentation cStringUsingEncoding:NSUTF8StringEncoding]);

        objectRepresentation = NSStringFromAnyObject( conststr );
        printf("%sn", [objectRepresentation cStringUsingEncoding:NSUTF8StringEncoding]);

        // UIKit

        objectRepresentation = NSStringFromAnyObject( CGRectMake(0, 0, 200, 200) );
        printf("%sn", [objectRepresentation cStringUsingEncoding:NSUTF8StringEncoding]);

        objectRepresentation = NSStringFromAnyObject( CGPointMake(0, 0) );
        printf("%sn", [objectRepresentation cStringUsingEncoding:NSUTF8StringEncoding]);

        objectRepresentation = NSStringFromAnyObject( CGSizeMake(0, 0) );
        printf("%sn", [objectRepresentation cStringUsingEncoding:NSUTF8StringEncoding]);
    }

    return 0;
}
nil/NULL
(__NSArrayI)(
    1
)
@(1)
(Class)__NSCFNumber
(SEL)main
(NSRange){0, 10}
nil/NULL
(void *)0x7fff88bd7948
(const void *)0x7fff88bd7948
YES
(double)1.240000
(float)1.240000
(int)1
(short)1
(long)1000
(long)100000000000
(char)c
(unsigned char)c
(char *)A string
(const char *)A string
(CGRect){0.000000, 0.000000, 200.000000, 200.000000}
(CGPoint){0.000000, 0.000000}
(CGSize){0.000000, 0.000000}
Program ended with exit code: 0

Unsupported types

Because of a hackery based on void * NSStringFromAnyObject does not work for the following types of data:

  • String literals
char str1[] = "12345";
char str2[6] = "12345";

NSLog(@"%@", NSStringFromAnyObject(str1));
NSLog(@"%@", NSStringFromAnyObject(str2));

=>

NSStringFromAnyObject: unsupported type: ([6c])
NSStringFromAnyObject: unsupported type: ([6c])
  • C arrays (excluding C strings)
int *nums = malloc(3 * sizeof(int));

nums[0] = 1;
nums[1] = 2;
nums[2] = 3;

NSLog(@"%@", NSStringFromAnyObject(nums));

=>

NSStringFromAnyObject: unsupported type: (^i)

All other typical types of objects are known to be supported.

Copyright

Copyright (c) 2014 Stanislaw Pankevich. See LICENSE for details.

Latest podspec

{
    "name": "NSStringFromAnyObject",
    "version": "0.0.4",
    "summary": "NSString representations for arbitrary C/Objective-C objects.",
    "homepage": "https://github.com/stanislaw/NSStringFromAnyObject",
    "license": {
        "type": "MIT"
    },
    "authors": {
        "Stanislaw Pankevich": "[email protected]"
    },
    "platforms": {
        "ios": "5.0",
        "osx": "10.7"
    },
    "source": {
        "git": "https://github.com/stanislaw/NSStringFromAnyObject.git",
        "tag": "0.0.4"
    },
    "source_files": "NSStringFromAnyObject/*.{h,m}",
    "private_header_files": [
        "NSStringFromAnyObject/Foundation.h",
        "NSStringFromAnyObject/C.h",
        "NSStringFromAnyObject/ObjC.h"
    ],
    "requires_arc": true
}

Pin It on Pinterest

Share This