#import "ViewController.h" #import "EOCPerson.h" #import "EOCLocation.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.view.backgroundColor = [UIColor whiteColor]; EOCPerson *person = [[EOCPerson alloc]initWithFirstName:@"Zou" secondName:@"Jie"]; NSLog(@"%@",person); EOCLocation *location = [[EOCLocation alloc]initWithTitle:@"HaiNan" latitude:15.00f longitude:10.00f]; NSLog(@"%@",location); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }
#import <Foundation/Foundation.h> /** * 自定義對象要想輸出更爲有用的信息,只須要覆寫description方法並將描述此對象的字符串返回便可 */ @interface EOCPerson : NSObject @property (nonatomic , copy , readonly) NSString *firstName; @property (nonatomic , copy , readonly) NSString *secondName; -(id)initWithFirstName:(NSString *)firstName secondName:(NSString *)secondName; @end #import "EOCPerson.h" @implementation EOCPerson -(id)initWithFirstName:(NSString *)firstName secondName:(NSString *)secondName { if ((self = [super init])){ _firstName = [firstName copy]; _secondName = [secondName copy]; } return self; } -(NSString *)description{ return [NSString stringWithFormat:@"%@ %@", _firstName,_secondName]; } //調試時 po命令的打印 -(NSString *)debugDescription { return [NSString stringWithFormat:@"<%@: %p,\"%@ %@\">",[self class],self,_firstName,_secondName]; } @end
#import <Foundation/Foundation.h> @interface EOCLocation : NSObject @property (nonatomic , copy , readonly) NSString *title; @property (nonatomic , assign , readonly) float latitude; @property (nonatomic , assign , readonly) float longitude; -(id)initWithTitle:(NSString *)title latitude:(float) latitude longitude:(float) longitude; @end #import "EOCLocation.h" @implementation EOCLocation -(id)initWithTitle:(NSString *)title latitude:(float)latitude longitude:(float)longitude { if ((self = [super init])){ _title = [title copy]; _latitude = latitude; _longitude = longitude; } return self; } -(NSString *)description { return [NSString stringWithFormat:@"<%@: %p, %@>",[self class],self,@{@"title":_title,@"latitude":@(_latitude),@"longitude":@(_longitude)}]; } @end