标签:
//对象方法封装结构体
1 #import <Foundation/Foundation.h> 2 typedef struct 3 { 4 char *name; 5 int age; 6 }Student; 7 8 @interface CZValue : NSObject 9 @property (nonatomic,assign)Student stu; 10 - (instancetype)valueWithStudent:(Student)stu; 11 @end 12 13 14 15 16 17 18 19 #import "CZValue.h" 20 21 @implementation CZValue 22 23 - (instancetype)valueWithStudent:(Student)stu 24 { 25 id val = [self.class new]; 26 [val setStu:stu]; 27 return val; 28 } 29 @end
//类方法创建结构体
1 #import <Foundation/Foundation.h> 2 3 struct Studnet 4 { 5 char *name; 6 int age; 7 char *stuNumber; 8 }; 9 @interface CZValue : NSObject 10 11 @property(nonatomic,assign)struct Studnet stu; 12 13 + (instancetype)valueWithStudent:(struct Studnet)stu; 14 @end 15 16 17 18 #import "CZValue.h" 19 20 @implementation CZValue 21 22 + (instancetype)valueWithStudent:(struct Studnet)stu 23 { 24 id v1 = [self new]; 25 [v1 setStu:stu]; 26 return v1; 27 } 28 @end
1 2.单例模式代码 2 3 // 4 // Person.h 5 6 #import <Foundation/Foundation.h> 7 8 @interface Person : NSObject 9 10 @property(nonatomic,copy)NSString *name; 11 @property(nonatomic,assign)int age; 12 13 14 + (instancetype)sharedPerson; 15 16 @end 17 18 19 20 + (instancetype)sharedPerson 21 { 22 return [[self alloc] init];//alloc 实际调用 allocWithZone 23 } 24 25 @end 26 27 28 手写情况: 29 //typedef struct _NSZone NSZone;//苹果自定义的 30 //1._NSZone,后3个字母小写。2.是类方法,不是对象方法 31 + (instancetype)allocWithZone:(struct _NSZone * )zone 32 { 33 static id instance = nil; 34 if (nil == instance) 35 { 36 instance = [super allocWithZone:zone]; 37 38 } 39 40 return instance; 41 } 42 43 + (instancetype)sharedPerson 44 { 45 return [[self alloc] init];//3.[ 位置也错了! 46 } 47 48 @end//4.位置居然放错了 5。没有在.h中声明
标签:
原文地址:http://www.cnblogs.com/Dast1/p/4790208.html