I. Overview
- When OC executes NSLog to output log information, it will call the description method
- The description method is divided into + description and -description methods
- + description is a class method, -description is an object method
Two description method demo
2.1 Class used (Person)
//Person.h
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property int age;
@property NSString *name;
@end
//Person.m
#import "Person.h"
@implementation Person
- (NSString *)description
{
return [NSString stringWithFormat:@"age=%d,name=%@",_age,_name];
}
+(NSString *)description
{
return @"---Person";
}
@end
2.2 Object method invocation (-description)
Person *p=[[Person alloc]init];
p.age=20;
p.name=@"jack";
NSLog(@"Person=%@",p);
2.3 Class method invocation (+ description)
Class c=[Person class];
NSLog(@"%@",[Person class]);
2.4 Other NSLog output
NSLog(@"%d",__LINE__);
NSLog(@"%s",__FILE__);
NSLog(@"%s",__func__);