Default String representation of an NSObject

In Actionscript, if you subclass Object, and you want a default string representation of your subclass, you just implement the method ‘toString’. For example the following:

package {
	public class Thing { 
		public var name:String;
		public function toString() : String {
			return (“Thing: “;
var thing:Thing = new Thing(); = “thing”
trace(thing);  //trace: Thing: thing

In Objective C, it’s very similar, however instead of ‘toString’ you use ‘description’.

#import <Foundation/Foundation.h>

@interface Thing : NSObject
@property (strong,nonatomic) NSString *name;

@implementation Thing
- (NSString *)description {
    return [NSString stringWithFormat: @"Thing: %@",];


Thing *thing = [[Thing alloc] init]; = @"thing"
NSLog(@"Thing: %@",thing);//log: Thing: thing

iOS development with Swift - book: video course:

Tagged with: ,
Posted in Objective C

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: