iOS Category 和 Protocol 中的 Property 大家真的會了麼?

1、propertyatom

合成存取器: spa

@property的格式:3d

 1 @property (修飾列表) 變量類型 變量名; 日誌

 

Xcode4.4以前:code

@property使編譯器自定生成set/get方法聲明。對象

@synthesize自動生成set/get方法的實現blog

@synthesize還會自動生成私有成員變量ci

 

Xcode4.4之後:get

不用再寫@synthesize,編譯器經過@property就能給咱們生成set/get方法的聲明和實現,默認生成成員變量:_propertyName編譯器

用@property生成的成員變量是私有的。

 當咱們想改變默認的成員變量名時,@synthesize age = newName;‘

若是子類想訪問父類的成員變量,

一、經過set/get方法

二、顯示的聲明成員變量

**進入正題**

首先聲明:

category和protocol能夠添加方法

category 和 protocol中能夠添加@property 關鍵字

2、關於protocol中的property

在protocol中添加property時,其實就是聲明瞭 getter 和 setter 方法,在實現這個protocol協議的類中,咱們要本身手動添加實例變量,而且須要實現setter/getter方法

僞代碼:

main.m

1 Student *student = [[Student alloc] init];
2         
3 student.name = @"彭爸爸";
4         
5 NSLog(@"%@", student.name);
6         

person.h

設置協議,與其中的property

 1 //
 2 //  Person.h
 3 //  類別 和 協議
 4 //
 5 //  Created by ma c on 16/5/19.
 6 //  Copyright © 2016年 彭盛凇. All rights reserved.
 7 //
 8 
 9 #import <Foundation/Foundation.h>
10 
11 @protocol personDelegate <NSObject>
12 
13 @property (nonatomic, copy) NSString *name;
14 
15 @end
16 
17 @interface Person : NSObject
18 
19 @end

student.h

生成成員變量,_name

 1 //
 2 //  PersonSon.h
 3 //  類別 和 協議
 4 //
 5 //  Created by ma c on 16/5/19.
 6 //  Copyright © 2016年 彭盛凇. All rights reserved.
 7 //
 8 
 9 #import <Foundation/Foundation.h>
10 #import "Person.h"
11 
12 @interface Student : NSObject <personDelegate>
13 {
14     //***聲明成員變量***
15      NSString *_name;
16 }
17 
18 
19 @end

student.m

這裏寫了兩種方法,一種是自動實現setter/gerter,一種爲手動setter/gerter

 1 //
 2 //  PersonSon.m
 3 //  類別 和 協議
 4 //
 5 //  Created by ma c on 16/5/19.
 6 //  Copyright © 2016年 彭盛凇. All rights reserved.
 7 //
 8 
 9 #import "Student.h"
10 
11 @implementation Student
12 
13 //------------------自動----------------------//
14 //@synthesize 
15 //
16 //編譯器期間,讓編譯器自動生成getter/setter方法。
17 //
18 //當有自定義的存或取方法時,自定義會屏蔽自動生成該方法
19 
20 //自動生成setter/getter方法
21 
22 //@synthesize name;
23 
24 
25 
26 //------------------手動----------------------//
27 
28 //@dynamic
29 //
30 //告訴編譯器,不自動生成getter/setter方法,避免編譯期間產生警告
31 //
32 //而後由本身實現存取方法
33 
34 //實現Person中定義的屬性name的set方法
35 - (void)setName:(NSString *)name {
36     _name = name;
37     
38 }
39 
40 //實現Person中定義的屬性name的get方法
41 - (NSString *)name {
42     return _name;
43 }
44 
45 @end

打印Log日誌爲:

說明成功實現property的基本功能,並能夠實現賦值與取值操做

 

3、category中的property

 

在category中添加property時, 在@implentation添加 getter 和 setter方法時, 因爲category不能添加實例變量

 

1)使用臨時全局變量來替代成員變量

首先聲明:Person沒有name屬性

main.m

導入頭文件:

#import "Person+Extension.h"

1 Person *person = [[Person alloc] init];
2         
3 person.name = @"peng";
4         
5 NSLog(@"%@", person.name);

Person+Extension.h

 1 //
 2 //  Person+Extension.h
 3 //  類別 和 協議
 4 //
 5 //  Created by ma c on 16/5/19.
 6 //  Copyright © 2016年 彭盛凇. All rights reserved.
 7 //
 8 
 9 #import "Person.h"
10 
11 @interface Person (Extension)
12 
13 @property (nonatomic, strong) NSString *name;
14 
15 @end

Person+Extension.m

 1 //
 2 //  Person+Extension.m
 3 //  類別 和 協議
 4 //
 5 //  Created by ma c on 16/5/19.
 6 //  Copyright © 2016年 彭盛凇. All rights reserved.
 7 //
 8 
 9 #import "Person+Extension.h"
10 #import <objc/runtime.h>
11 
12 //***臨時變量***
13 static NSString *_name;
14 
15 @implementation Person (Extension)
16 
17 - (void)setName:(NSString *)name {
18     _name = name;
19 }
20 
21 - (NSString *)name {
22     return _name;
23 }
24 @end

2)使用runtime 關聯對象 實現成員變量

除了Person+Extension.m 其餘與 第一種方法一致

runtime Person+Extension.m

 1 @implementation Person (Extension)
 2 
 3 - (void)setName:(NSString *)name{
 4     objc_setAssociatedObject(self,@selector(name),name,OBJC_ASSOCIATION_RETAIN_NONATOMIC);
 5 }
 6 
 7 - (NSString *)name{
 8     
 9     NSString *n = objc_getAssociatedObject(self, @selector(name));
10     return n;
11 }

兩種方式打印Log日誌相同,同時爲:

相關文章
相關標籤/搜索