objective-c的description测试

#import 

@interface Human : NSObject  {
    int age;
    NSString *name;
    Human *child;
}
@property int age;
@property (copy)NSString *name;
@property (retain)Human *child;
@end

@implementation Human
@synthesize name;
@synthesize age;
@synthesize child;

/*一般情况下,一个自定义类我们在用%@输出的时候,给出的是一个内存地址,
我们在该类的.m文件里重写description方法,来修改输出内容*/

#if 1
-(NSString *)description  {
    NSString *des = [NSString stringWithFormat:@"->%@,%d,%@",name,age,child];
    return des;
}
#endif

@end

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

    NSAutoreleasePool *pool =[[NSAutoreleasePool alloc] init];

    Human *human1=[[Human alloc]init];
    Human *human2=[[Human alloc]init];
    human2.name=@"jeck";
    human2.age=23;
    
    human1.child=human2;
    human1.name=@"holydancer";
    human1.age=22;
    
    NSLog(@"%@",human1);

    [pool release];
    return 0;
}

结果:

2016-12-02 11:46:52.998 test[7289] ->holydancer,22,->jeck,23,(null)

你可能感兴趣的:(ios)