[Description of NSObject]

Hi, you can give an example of using this method.

+(NSString *)description 

Am I using a description with an instance of NSObject (any object) or NSString?

or am I using without an instance, directly using NSObject (any object) or NSString?

+4
source share
5 answers

The instance description gives you information about the specific instance that you created.

- (NSString *) description

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

Gives you information about this instance (memory location, etc.)

On the other hand:

+ (NSString *) description

 [NSString description]; 

Gives you information about the NSString class.

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

+16
source

Say we have:

 @interface randomObject : NSObject { NSString *yourString; } 

and somewhere:

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

then we can override randomObject as follows:

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

after that we can call NSLog with our NSObject:

 -(void)viewDidLoad { randomObject *ourObj; ourObj = [[randomObject alloc] init]; NSLog(@"%@", ourObj); // this will output "random text" } 
+4
source

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

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

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

this method will be called, and you will get a string that the class uses to describe itself.

On the other hand, the NSObject protocol declares the required instance method -[id<NSObject> description] , which will return a string describing 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 NSObject subclasses inherit both of these methods, and they can be overridden, like any other. Note, for example, that NSDictionary and NSArray themselves format and send description to the objects that they contain.

It should also be noted that when using NSLog() the %@ format specifier calls description to send it to the argument (whether it be a class object or an instance).

+4
source

Most commonly used call

 - (NSString *)description; 

Used for regular instances, not classes. It can be overridden in custom classes to provide detailed information about the object. If you try to access the class as a string, the description method will be called automatically.

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

You can use it on classes just as you stated.

 [NSArray description]; 
+2
source
 +(NSString *)description 

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

+1
source

All Articles