[NSObject description]

10,144

Solution 1

The description of the instance gives you information about the specific instance you have created.

- (NSString *)description;

NSString *string = [NSString alloc] initwithString:@"aString"]];
[string description];

Gives you information about this instance (location in memory etc)

On the other side:

+ (NSString *)description;

[NSString description];

Gives you information about the class NSString.

The same rules apply to all NSObject subclasses and other classes that conform to NSObject protocol such NSArray, NSDictionary *NSProxy* etc

Solution 2

You seem to mainly be confused about the difference between class and instance methods.

NSObject declares the class method +[NSObject description], which, as the docs tell you "Returns a string that represents the contents of the receiving class.". If you send the message description to a class object, like so:

[NSArray description];
[NSNumber description];
[[someObject class] description];

this method will be called and you'll get the string the class uses to describe itself.

On the other hand, the NSObject protocol declares a required instance method -[id<NSObject> description], which will return "a string that describes the contents of the receiver". When you send this to an instance, you get a string representing it:

NSNumber * n = [NSNumber numberWithInt:10];
[n description];
NSArray * arr = [NSArray arrayWithObjects:@"Lemon", @"curry", @"?", nil];
[arr description];
NSDicitonary * d = [NSDictionary dictionaryWithObject:arr forKey:n];
[d description]; 

All subclasses of NSObject inherit both of these methods, and they can be overridden just like any other. Notice, for example, that NSDictionary and NSArray format themselves and send description to the objects they contain.

It should also be noted that, when using NSLog(), the %@ format specifier causes description to be sent to its argument (whether it's a class object or an instance).

Solution 3

Let's say we have:

@interface randomObject : NSObject
{
    NSString  *yourString;
}

and somewhere:

yourString = [[NSString alloc] initWithString:@"random text"];

then we can override randomObject like this...

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

after we done this we can call a NSLog with our NSObject:

-(void)viewDidLoad {
    randomObject *ourObj;

    ourObj = [[randomObject alloc] init];

    NSLog(@"%@", ourObj); //  this will output "random text"
}

Solution 4

The call most normally used it actually

- (NSString *)description;

It is used on normally instances, not classes. It can be overridden in custom classes to provide detailed information about an object. If you attempt to access a class as as string, the description method will automatically be called.

NSLog(@"array: %@", array);               //Identical
NSLog(@"array: %@", [array description]); //Identical

You can use it on classes just as you stated

[NSArray description];

Solution 5

+(NSString *)description

Is used mainly for debug and is used by instances. It allows to print a description of the object.

Share:
10,144
Michael C
Author by

Michael C

please delete me

Updated on June 27, 2022

Comments

  • Michael C
    Michael C almost 2 years

    Hello can you give me an example of the usage of this method

    +(NSString *)description
    

    Do I use description with an instance of a NSObject (any kind of object) or NSString?

    or do I use without an instance, directly using NSObject (any kind of object) or NSString?