iOS開發大招-使用運行時runtime方法給一個類添加屬性

來源:互聯網
上載者:User

標籤:ios開發進階   運行時添加屬性   runtime大招   類目與runtime結合使用   封裝af和 hud   

看過一些第三方開源類庫的原始碼,經常發現他們 給一個 類 添加了一個原本不存在的屬性,

比如PPrealSideController 就給 UIViewController添加了一個 self.pprealSideController的屬性?

他是如何?的呢?


1.基本的實現思路


首先我們需要瞭解,實際上 在我們使用  類似於self.newProperty的語句的時候,

根據點文法的規則實際上是調用的  setNewProperty方法,和  newProperty方法,

那我們可以知道他肯定是給一個  UIViewController添加了一個類目,然後 給它添加了

set和get方法(同上),所以看到這裡我們可以知道 他添加的  self.newProperty實際上

只是一個形式上的屬性,當你使用 _newProperry的時候是不可以的!

所以這種形式的‘屬性‘ 只能使用點的形式去調用.


2.使用runtime方法來建立關聯


雖然我們知道了可以使用類目給一個類添加兩個方法,也就是間接的屬性,

但是如何把我們需要儲存的 屬性值  和它關聯起來呢?

在  <objc/runtime.h>運行時架構裡給我們提供了對應的方法,可以讓我們 用一個靜態 key

給 一塊記憶體 賦值,方然也可以使用這個 key 擷取到對應的 值


對應的方法是:

    objc_setAssociatedObject(self, &StaticManagerKey,                             manager,                             OBJC_ASSOCIATION_RETAIN);


和 

objc_getAssociatedObject(self, &StaticManagerKey);


使用樣本:

我們給一個 UIViewController類添加一個 manager屬性:

我們需要建立一個 UIViewController類目,詳細代碼如下:

.h

////  UIViewController+BBManager.h//  HuanYouWang////  Created by MBinYang on 15/4/23.//  Copyright (c) 2015年 cc.huanyouwang. All rights reserved.//#import <UIKit/UIKit.h>#import "BBRequest.h"@interface UIViewController ()@property(strong,nonatomic)BBRequest *manager;@end@interface UIViewController (BBManager)@end

可以看到 我們給UIViewController添加了一個 manager屬性 ,實際上這個屬性的 作用就是 給我們在.h中提供 一個 setManager方法,和 manager方法;


.m

////  UIViewController+BBManager.m//  HuanYouWang////  Created by MBinYang on 15/4/23.//  Copyright (c) 2015年 cc.huanyouwang. All rights reserved.//#import "UIViewController+BBManager.h"#import <objc/runtime.h>@implementation UIViewController (BBManager)static char StaticManagerKey;- (void)setManager:(BBRequest *)manager{    [self willChangeValueForKey:@"StaticManagerKey"];    objc_setAssociatedObject(self, &StaticManagerKey,                             manager,                             OBJC_ASSOCIATION_RETAIN);    [self didChangeValueForKey:@"StaticManagerKey"];}- (BBRequest *)manager{    return objc_getAssociatedObject(self, &StaticManagerKey);}@end

可以看到 ,我們在  .m中,實現了  set,get方法.


使用樣本,我們在一個 UIViewController的子類中測試   :

在TestViewController中 匯入

UIViewController+BBManager.h

然後測試:

self.manager = [[BBRequest alloc]init];//調用 set方法    [self.manager doSomething];//調用  get方法

可以看到 我們 為 一個  UIViewController成功添加了一個 屬性  manager.


更多原文:http://blog.csdn.net/yangbingbinga


下一篇:使用運行時來高度封裝 AF和 HUD


總結:當我們使用 運行時方法給一個 類添加屬性之後,我們就可以非常方便的使用它了







iOS開發大招-使用運行時runtime方法給一個類添加屬性

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.