OC development-description method (34)

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__);
Published 362 original articles · 118 praises · 530,000 views

Guess you like

Origin blog.csdn.net/Calvin_zhou/article/details/105445129