Description method [dead loop]

Time:2021-3-5
#import "MaTsonga .h"


@implementation MaTsonga

-(NSString *)description{
    return [NSString stringWithFormat:@"{name:%@,age:%i}",self.name,self.age];
}

@end

#import "MaTsonga .h"


int main(int argc, const char * argv[]) {

    MaTsonga *ma=[[MaTsonga alloc]init];

    ma.name= @"Is it like being silly";
    ma.age=24;
    
    Nslog (@ "% @", MA); // the object description method will be called to return the corresponding description information
    
    
    return 0;
}

Note that the format character in nslog is% @. When% @ is used to output an object, objc will call the description of each object to return the corresponding information for output. By default, if we do not rewrite the description method, the output content is the class name and address.

It should be emphasized that do not print out self in the description, because when self is output, the description method of the object will be called, which will cause a dead loop.