iOS Extension Category Protrol 例子理解
直接上程式碼
先讀懂程式碼才能更好的理解含義。
目錄:
Person類沒有暴漏任何方法:
//
// Person.h
// ExtensionCateGoryProtrolDemo
//
// Created by apple on 2017/12/14.
// Copyright © 2017年 ZY. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Study.h"
@interface Person : NSObject<Study>
@end
//
// Person.m
// ExtensionCateGoryProtrolDemo
//
// Created by apple on 2017/12/14.
// Copyright © 2017年 ZY. All rights reserved.
//
#import "Person.h"
#import <objc/runtime.h>
//這也是類擴充套件
@interface Person ()
@property(nonatomic,assign) int age;
@end
@implementation Person
-(void)logAge{
self.age = 18;
NSLog(@"%d",self.age);
}
-(void)Eat{
NSLog(@"Eat");
}
-(void)Study{
NSLog(@"Study");
}
-(void)setFriendName:(NSString *)name{
objc_setAssociatedObject(self, @"friendName", name,OBJC_ASSOCIATION_COPY_NONATOMIC);
}
-(NSString *)friendName{
return objc_getAssociatedObject(self, @"friendName");
}
@end
Category用runtime方法實現了可以設定屬性:
//
// Person+Run.h
// ExtensionCateGoryProtrolDemo
//
// Created by apple on 2017/12/14.
// Copyright © 2017年 ZY. All rights reserved.
//
#import "Person.h"
@interface Person (Run)
@property(nonatomic,strong)NSString * name;
-(void)Run;
@end
//
// Person+Run.m
// ExtensionCateGoryProtrolDemo
//
// Created by apple on 2017/12/14.
// Copyright © 2017年 ZY. All rights reserved.
//
#import "Person+Run.h"
#import <objc/runtime.h>
@implementation Person (Run)
-(void)Run{
NSLog(@"Run");
}
-(void)setName:(NSString *)name{
objc_setAssociatedObject(self, @"name", name,OBJC_ASSOCIATION_COPY_NONATOMIC);
}
-(NSString *)name{
return objc_getAssociatedObject(self, @"name");
}
@end
單獨的Extension檔案相當於一個需要自己實現的Category(本例子已經在Person類實現):
//
// Person+Eat.h
// ExtensionCateGoryProtrolDemo
//
// Created by apple on 2017/12/14.
// Copyright © 2017年 ZY. All rights reserved.
//
#import "Person.h"
@interface Person ()
@property(nonatomic,copy)NSString * friendName;
-(void)logAge;
-(void)Eat;
@end
Protocol和Extension一樣需要別人實現,我理解的區別是Extension必須要你擴充套件的類實現,而protocol誰遵守協議誰實現:
//
// Study.h
// ExtensionCateGoryProtrolDemo
//
// Created by apple on 2017/12/14.
// Copyright © 2017年 ZY. All rights reserved.
//
#import <Foundation/Foundation.h>
@protocol Study <NSObject>
-(void)Study;
@end
呼叫類:
//
// ViewController.m
// ExtensionCateGoryProtrolDemo
//
// Created by apple on 2017/12/14.
// Copyright © 2017年 ZY. All rights reserved.
//
#import "ViewController.h"
#import "Person+Run.h"
#import "Extension/Person+Eat.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
Person * Mryang = [Person new];
Mryang.name = @"Mryang";
Mryang.friendName =@"MrsLu";
NSLog(@"%@",Mryang.name);
NSLog(@"%@",Mryang.friendName);
[Mryang logAge];
[Mryang Run];
[Mryang Eat];
[Mryang Study];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
列印結果可想而知。見demo
相關文章
- IOS category 與 extensioniOSGo
- Extension,CategoryGo
- 談談Category和ExtensionGo
- iOS分類(category)、類擴充套件(extension)、繼承的區別iOSGo套件繼承
- iOS學習筆記2(結合專案)--Category和Extension的區別iOS筆記Go
- Objective-C 類別(category)和擴充套件(Extension)ObjectGo套件
- iOS底層原理-CategoryiOSGo
- iOS設計模式——CategoryiOS設計模式Go
- 深入理解Objective-C:CategoryObjectGo
- iOS問題整理03----CategoryiOSGo
- iOS套路面試題之CategoryiOS面試題Go
- 關於iOS Class Category的整理iOSGo
- 【iOS】category重寫方法的呼叫iOSGo
- iOS使用Category新增@property變數iOSGo變數
- iOS runtime 給 Category 加屬性iOSGo
- 玩轉iOS開發:iOS 8 新特性《Share Extension》iOS
- iOS桌面小外掛 Widget ExtensioniOS
- 小例子理解多型多型
- 玩轉iOS開發:iOS 10 新特性《Xcode Extension》iOSXCode
- iOS 簡訊攔截 Message Filter ExtensioniOSFilter
- iOS之widget開發(Today Extension)iOS
- iOS底層原理總結 - Category的本質iOSGo
- iOS中擴充套件機制Category和associativeiOS套件Go
- iOS Extension擴充套件開啟宿主應用iOS套件
- 玩轉iOS開發:iOS 10 新特性《Siri Kit Intents Extension UI》iOSIntentUI
- IOS測試用例例子iOS
- iOS 開發:『Runtime』詳解(三)Category 底層原理iOSGo
- Sqlserver鎖升級的理解和例子SQLServer
- Category探索Go
- hello categoryGo
- man categoryGo
- 面向 Extension 開發 ? Today Extension
- Category 特性在 iOS 元件化中的應用與管控GoiOS元件化
- 一個理解wait()與notify()的例子AI
- App ExtensionAPP
- Category – 簡介Go
- 分類-CategoryGo
- OC Category、AssociatedObjectGoObject