Skip to content

Commit

Permalink
add code and spec file
Browse files Browse the repository at this point in the history
  • Loading branch information
FlameGrace committed Jun 26, 2017
1 parent db87ad2 commit 427c268
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
.DS_Store
23 changes: 23 additions & 0 deletions MultiDelegateOC.podspec
@@ -0,0 +1,23 @@
#
# Be sure to run `pod spec lint MultiDelegateOC.podspec' to ensure this is a
# valid spec and to remove all comments including this before submitting the spec.
#
# To learn more about Podspec attributes see http://docs.cocoapods.org/specification.html
# To see working Podspecs in the CocoaPods repo see https://github.com/CocoaPods/Specs/
#

Pod::Spec.new do |s|
s.name = "MultiDelegateOC"
s.version = "0.0.1"
s.summary = "It can provide a multi delegate model for iOS and OSX."
s.homepage = "https://github.com/FlameGrace/MultiDelegateOC"
s.license = "BSD"
s.author = { "FlameGrace" => "flamegrace@hotmail.com" }
s.ios.deployment_target = '7.0'
s.osx.deployment_target = '10.9'
s.watchos.deployment_target = '2.0'
s.tvos.deployment_target = '9.0'
s.source = { :git => "https://github.com/FlameGrace/MultiDelegateOC.git", :tag => "0.0.1" }
s.source_files = "MultiDelegateOC", "MultiDelegateOC/**/*.{h,m}"
s.public_header_files = "MultiDelegateOC/**/*.h"
end
15 changes: 15 additions & 0 deletions MultiDelegateOC/MDWeakObject.h
@@ -0,0 +1,15 @@
//
// MDWeakObject.h
// Combin
//
// Created by Flame Grace on 2017/6/26.
// Copyright © 2017年 flamegrace. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface MDWeakObject : NSObject

@property (weak, nonatomic) id object;

@end
13 changes: 13 additions & 0 deletions MultiDelegateOC/MDWeakObject.m
@@ -0,0 +1,13 @@
//
// MDWeakObject.m
// Combin
//
// Created by Flame Grace on 2017/6/26.
// Copyright © 2017年 flamegrace. All rights reserved.
//

#import "MDWeakObject.h"

@implementation MDWeakObject

@end
28 changes: 28 additions & 0 deletions MultiDelegateOC/NSObject+MultiDelegate.h
@@ -0,0 +1,28 @@
//
// NSObject+MultiDelegate.h
// MultiDelegate
//
// Created by Flame Grace on 16/11/17.
// Copyright © 2016年 flamegrace@hotmail.com. All rights reserved.
//

#import <Foundation/Foundation.h>

//获取单个代理对象对其进行操作
typedef void(^MultiDelegateOperateBlack)(id delegate);


/*
此类扩展能实现多代理
需要委托代理执行方法时,调用operateDelegates:方法可在相应block块中获取到单个代理,对其进行操作
*/
@interface NSObject (MultiDelegate)

//添加一个代理
- (void)addMultiDelegate:(id)delegate;
//移除一个代理
- (void)removeMultiDelegate:(id)delegate;
//操作代理
- (void)operateMultiDelegates:(MultiDelegateOperateBlack)operate;

@end
88 changes: 88 additions & 0 deletions MultiDelegateOC/NSObject+MultiDelegate.m
@@ -0,0 +1,88 @@
//
// NSObject+MultiDelegate.m
// MultiDelegate
//
// Created by Flame Grace on 16/11/17.
// Copyright © 2016年 flamegrace@hotmail.com. All rights reserved.
//

#import "NSObject+MultiDelegate.h"
#import "MDWeakObject.h"
#import <objc/runtime.h>


@implementation NSObject (MultiDelegate)

//使用动态运行时添加数组对象multiDelegates,保存代理
- (void)setMultiDelegates:(NSMutableArray<MDWeakObject *> *)multiDelegates
{
objc_setAssociatedObject(self, @selector(multiDelegates), multiDelegates, OBJC_ASSOCIATION_RETAIN);
}

- (NSMutableArray<MDWeakObject *> *)multiDelegates
{
NSMutableArray *multiDelegates = objc_getAssociatedObject(self,@selector(multiDelegates));
if(multiDelegates == nil)
{
multiDelegates = [[NSMutableArray alloc]init];
objc_setAssociatedObject(self, @selector(multiDelegates), multiDelegates, OBJC_ASSOCIATION_RETAIN);
}
return multiDelegates;
}

- (void)addMultiDelegate:(id)delegate
{
if(delegate == nil)return;

__block BOOL exist = NO;

__weak typeof(self) weakSelf = self;

[self.multiDelegates enumerateObjectsUsingBlock:^(MDWeakObject * _Nonnull delegate, NSUInteger idx, BOOL * _Nonnull stop) {
if(delegate.object)
{
if([delegate.object isEqual:delegate])
{
exist = YES;
}
}
else
{
[weakSelf.multiDelegates removeObject:delegate];
}
}];

if(!exist)
{
MDWeakObject *newDelegate = [[MDWeakObject alloc]init];
newDelegate.object =delegate;
[self.multiDelegates addObject:newDelegate];
}

}

- (void)removeMultiDelegate:(id)delegate
{
if(delegate == nil)return;

__weak typeof(self) weakSelf = self;

[self.multiDelegates enumerateObjectsUsingBlock:^(MDWeakObject * _Nonnull delegate, NSUInteger idx, BOOL * _Nonnull stop) {
if([delegate.object isEqual:delegate])
{
[weakSelf.multiDelegates removeObject:delegate];
}
}];

}

- (void)operateMultiDelegates:(MultiDelegateOperateBlack)operate
{
[self.multiDelegates enumerateObjectsUsingBlock:^(MDWeakObject * _Nonnull delegate, NSUInteger idx, BOOL * _Nonnull stop) {
operate(delegate.object);
}];
}



@end

0 comments on commit 427c268

Please sign in to comment.