Skip to content

Commit

Permalink
0.0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
casatwy committed Jun 5, 2017
1 parent fb64e34 commit c1debf1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 38 deletions.
2 changes: 1 addition & 1 deletion CTMediator.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Pod::Spec.new do |s|
#

s.name = "CTMediator"
s.version = "0.0.4"
s.version = "0.0.5"
s.summary = "CTMediator."

# This description is used to generate tags and improve search results.
Expand Down
55 changes: 18 additions & 37 deletions CTMediator/CTMediator/CTMediator.m
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ - (id)performTarget:(NSString *)targetName action:(NSString *)actionName params:

if (target == nil) {
// 这里是处理无响应请求的地方之一,这个demo做得比较简单,如果没有可以响应的target,就直接return了。实际开发过程中是可以事先给一个固定的target专门用于在这个时候顶上,然后处理这种请求的
NSAssert(NO, @"target not founded");
return nil;
}

Expand All @@ -89,75 +88,57 @@ - (id)performTarget:(NSString *)targetName action:(NSString *)actionName params:
}

if ([target respondsToSelector:action]) {
//#pragma clang diagnostic push
//#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
// return [target performSelector:action withObject:params];
//#pragma clang diagnostic pop
return [self safePerformSelector:target action:action params:params];
return [self safePerformAction:action target:target params:params];
} else {
// 有可能target是Swift对象
actionString = [NSString stringWithFormat:@"Action_%@WithParams:", actionName];
action = NSSelectorFromString(actionString);
if ([target respondsToSelector:action]) {
//#pragma clang diagnostic push
//#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
// return [target performSelector:action withObject:params];
//#pragma clang diagnostic pop
return [self safePerformSelector:target action:action params:params];
return [self safePerformAction:action target:target params:params];
} else {
// 这里是处理无响应请求的地方,如果无响应,则尝试调用对应target的notFound方法统一处理
SEL action = NSSelectorFromString(@"notFound:");
if ([target respondsToSelector:action]) {
//#pragma clang diagnostic push
//#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
// return [target performSelector:action withObject:params];
//#pragma clang diagnostic pop
return [self safePerformSelector:target action:action params:params];
return [self safePerformAction:action target:target params:params];
} else {
// 这里也是处理无响应请求的地方,在notFound都没有的时候,这个demo是直接return了。实际开发过程中,可以用前面提到的固定的target顶上的。
[self.cachedTarget removeObjectForKey:targetClassString];
NSAssert(NO, @"target not founded");
return nil;
}
}
}
}

- (id)safePerformSelector:(NSObject *)target action:(SEL)action params:(NSDictionary *)params
- (void)releaseCachedTargetWithTargetName:(NSString *)targetName
{
NSString *targetClassString = [NSString stringWithFormat:@"Target_%@", targetName];
[self.cachedTarget removeObjectForKey:targetClassString];
}

#pragma mark - private methods
- (id)safePerformAction:(SEL)action target:(NSObject *)target params:(NSDictionary *)params
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
NSMethodSignature* methodSig = [target methodSignatureForSelector:action];
if(methodSig == nil){
NSAssert(NO, @"target not founded");
if(methodSig == nil) {
return nil;
}
const char* retType = [methodSig methodReturnType];
if(strcmp(retType, @encode(id)) == 0)
{
if(strcmp(retType, @encode(id)) == 0) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
return [target performSelector:action withObject:params];
}
else if(strcmp(retType, @encode(void)) == 0){
#pragma clang diagnostic pop
} else if(strcmp(retType, @encode(void)) == 0) {
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:
methodSig];
[invocation setArgument:&params atIndex:2];
[invocation setSelector:action];
[invocation setTarget:target];
[invocation invoke];
return nil;
}
else{
NSAssert(NO, @"target not founded");
NSLog(@"-[%@ performSelector:@selector(%@)] shouldn't be used. The selector doesn't return an object or void", [target class], NSStringFromSelector(action));
} else {
return nil;
}
#pragma clang diagnostic pop
}

- (void)releaseCachedTargetWithTargetName:(NSString *)targetName
{
NSString *targetClassString = [NSString stringWithFormat:@"Target_%@", targetName];
[self.cachedTarget removeObjectForKey:targetClassString];
}

#pragma mark - getters and setters
Expand Down

0 comments on commit c1debf1

Please sign in to comment.