[关闭]
@fiy-fish 2015-07-15T20:04:11.000000Z 字数 809 阅读 1242

day04-02-description

Objective-c


  1. // main.m
  2. // day04-02-description
  3. //
  4. // Created by Aaron on 15/7/6.
  5. // Copyright (c) 2015年 Aaron. All rights reserved.
  6. //
  7. #import <Foundation/Foundation.h>
  8. #import "Person.h"
  9. int main(int argc, const char * argv[]) {
  10. @autoreleasepool {
  11. Person *p = [[Person alloc] init];
  12. NSLog(@"%p",p);
  13. NSLog(@"%@",p);
  14. NSLog(@"%@",[p description]);
  15. //NSNumber 作为介质来将基本类型转为对象类型或者将对象类型转为基本类型
  16. NSNumber *num1 = [NSNumber numberWithInt:10];
  17. int num2 = [num1 intValue];
  18. NSLog(@"num2--->%d",num2);
  19. NSLog(@"%@",num1);
  20. BOOL rec = [num1 boolValue];
  21. NSLog(@"%d",rec);
  22. }
  23. return 0;
  24. }

  1. #import <Foundation/Foundation.h>
  2. @interface Person : NSObject
  3. {
  4. NSString *_name;
  5. NSInteger _age;
  6. }
  7. @end

  1. #import "Person.h"
  2. @implementation Person
  3. -(instancetype)init
  4. {
  5. if(self = [super init])
  6. {
  7. _name = @"蜡笔小新";
  8. _age = 3;
  9. }
  10. return self;
  11. }
  12. -(NSString *)description
  13. {
  14. return [NSString stringWithFormat:@"%@的年龄是%ld",_name,_age];
  15. }
  16. @end
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注