iOS Extension Category Protrol 例子理解

ZY_FlyWay發表於2017-12-15


直接上程式碼

先讀懂程式碼才能更好的理解含義。


目錄





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

相關文章