Skip to content

Instantly share code, notes, and snippets.

@smugen
Created January 27, 2015 08:54
Show Gist options
  • Save smugen/6710ec5741795451f048 to your computer and use it in GitHub Desktop.
Save smugen/6710ec5741795451f048 to your computer and use it in GitHub Desktop.
Add `performSelector` back to swift
//
// NSObjectExtension.h
// KeyOUcare
//
// Created by CYWang on 2015/1/27.
// Copyright (c) 2015年 OUcare. All rights reserved.
//
#ifndef KeyOUcare_NSObjectExtension_h
#define KeyOUcare_NSObjectExtension_h
#import <Foundation/Foundation.h>
@interface NSObject (Swift)
/**
Sends a specified message to the receiver and returns the result of the message.
@param aSelector
A selector identifying the message to send. If aSelector is NULL, an NSInvalidArgumentException is raised.
@returns An object that is the result of the message.
*/
- (id)OUPerformSelector:(SEL)aSelector;
/**
Sends a message to the receiver with an object as the argument.
@param aSelector
A selector identifying the message to send. If aSelector is NULL, an NSInvalidArgumentException is raised.
@param anObject
An object that is the sole argument of the message.
@returns An object that is the result of the message.
*/
- (id)OUPerformSelector:(SEL)aSelector
withObject:(id)anObject;
/**
Sends a message to the receiver with two objects as arguments.
@param aSelector
A selector identifying the message to send. If aSelector is NULL, an NSInvalidArgumentException is raised.
@param anObject
An object that is the first argument of the message.
@param anotherObject
An object that is the second argument of the message
@returns An object that is the result of the message.
*/
- (id)OUPerformSelector:(SEL)aSelector
withObject:(id)anObject
withObject:(id)anotherObject;
/**
Invokes a method of the receiver on the current thread using the default mode after a delay.
@param aSelector
A selector that identifies the method to invoke. The method should not have a significant return value and should take a single argument of type id, or no arguments.
@param anArgument
The argument to pass to the method when it is invoked. Pass nil if the method does not take an argument.
@param delay
The minimum time before which the message is sent. Specifying a delay of 0 does not necessarily cause the selector to be performed immediately. The selector is still queued on the thread’s run loop and performed as soon as possible.
*/
- (void)OUPerformSelector:(SEL)aSelector
withObject:(id)anArgument
afterDelay:(NSTimeInterval)delay;
/**
Invokes a method of the receiver on the current thread using the specified modes after a delay.
@param aSelector
A selector that identifies the method to invoke. The method should not have a significant return value and should take a single argument of type id, or no arguments.
@param anArgument
The argument to pass to the method when it is invoked. Pass nil if the method does not take an argument.
@param delay
The minimum time before which the message is sent. Specifying a delay of 0 does not necessarily cause the selector to be performed immediately. The selector is still queued on the thread’s run loop and performed as soon as possible.
@param modes
An array of strings that identify the modes to associate with the timer that performs the selector. This array must contain at least one string. If you specify nil or an empty array for this parameter, this method returns without performing the specified selector.
*/
- (void)OUPerformSelector:(SEL)aSelector
withObject:(id)anArgument
afterDelay:(NSTimeInterval)delay
inModes:(NSArray *)modes;
/**
Invokes a method of the receiver on the main thread using the default mode.
@param aSelector
A selector that identifies the method to invoke. The method should not have a significant return value and should take a single argument of type id, or no arguments.
@param arg
The argument to pass to the method when it is invoked. Pass nil if the method does not take an argument.
@param wait
A Boolean that specifies whether the current thread blocks until after the specified selector is performed on the receiver on the main thread. Specify YES to block this thread; otherwise, specify NO to have this method return immediately.
If the current thread is also the main thread, and you specify YES for this parameter, the message is delivered and processed immediately.
*/
- (void)OUPerformSelectorOnMainThread:(SEL)aSelector
withObject:(id)arg
waitUntilDone:(BOOL)wait;
/**
Invokes a method of the receiver on the main thread using the specified modes.
@param aSelector
A selector that identifies the method to invoke. The method should not have a significant return value and should take a single argument of type id, or no arguments.
@param arg
The argument to pass to the method when it is invoked. Pass nil if the method does not take an argument.
@param wait
A Boolean that specifies whether the current thread blocks until after the specified selector is performed on the receiver on the main thread. Specify YES to block this thread; otherwise, specify NO to have this method return immediately.
If the current thread is also the main thread, and you pass YES, the message is performed immediately, otherwise the perform is queued to run the next time through the run loop.
@param array
An array of strings that identifies the modes in which it is permissible to perform the specified selector. This array must contain at least one string. If you specify nil or an empty array for this parameter, this method returns without performing the specified selector.
*/
- (void)OUPerformSelectorOnMainThread:(SEL)aSelector
withObject:(id)arg
waitUntilDone:(BOOL)wait
modes:(NSArray *)array;
/**
Invokes a method of the receiver on the specified thread using the default mode.
@param aSelector
A selector that identifies the method to invoke. The method should not have a significant return value and should take a single argument of type id, or no arguments.
@param thread
The thread on which to execute aSelector.
@param arg
The argument to pass to the method when it is invoked. Pass nil if the method does not take an argument.
@param wait
A Boolean that specifies whether the current thread blocks until after the specified selector is performed on the receiver on the specified thread. Specify YES to block this thread; otherwise, specify NO to have this method return immediately.
If the current thread and target thread are the same, and you specify YES for this parameter, the selector is performed immediately on the current thread. If you specify NO, this method queues the message on the thread’s run loop and returns, just like it does for other threads. The current thread must then dequeue and process the message when it has an opportunity to do so.
*/
- (void)OUPerformSelector:(SEL)aSelector
onThread:(NSThread *)thread
withObject:(id)arg
waitUntilDone:(BOOL)wait;
/**
Invokes a method of the receiver on the specified thread using the specified modes.
@param aSelector
A selector that identifies the method to invoke. It should not have a significant return value and should take a single argument of type id, or no arguments.
@param thread
The thread on which to execute aSelector. This thread represents the target thread.
@param arg
The argument to pass to the method when it is invoked. Pass nil if the method does not take an argument.
@param wait
A Boolean that specifies whether the current thread blocks until after the specified selector is performed on the receiver on the specified thread. Specify YES to block this thread; otherwise, specify NO to have this method return immediately.
If the current thread and target thread are the same, and you specify YES for this parameter, the selector is performed immediately. If you specify NO, this method queues the message and returns immediately, regardless of whether the threads are the same or different.
@param array
An array of strings that identifies the modes in which it is permissible to perform the specified selector. This array must contain at least one string. If you specify nil or an empty array for this parameter, this method returns without performing the specified selector.
*/
- (void)OUPerformSelector:(SEL)aSelector
onThread:(NSThread *)thread
withObject:(id)arg
waitUntilDone:(BOOL)wait
modes:(NSArray *)array;
/**
Invokes a method of the receiver on a new background thread.
@param aSelector
A selector that identifies the method to invoke. The method should not have a significant return value and should take a single argument of type id, or no arguments.
@param arg
The argument to pass to the method when it is invoked. Pass nil if the method does not take an argument.
*/
- (void)OUPerformSelectorInBackground:(SEL)aSelector
withObject:(id)arg;
@end
#endif
//
// NSObjectExtension.m
// KeyOUcare
//
// Created by CYWang on 2015/1/27.
// Copyright (c) 2015年 OUcare. All rights reserved.
//
#import "NSObjectExtension.h"
@implementation NSObject (Swift)
- (id)OUPerformSelector:(SEL)aSelector
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
return [self performSelector:aSelector];
#pragma clang diagnostic pop
}
- (id)OUPerformSelector:(SEL)aSelector
withObject:(id)anArgument
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
return [self performSelector:aSelector
withObject:anArgument];
#pragma clang diagnostic pop
}
- (id)OUPerformSelector:(SEL)aSelector
withObject:(id)anObject
withObject:(id)anotherObject
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
return [self performSelector:aSelector
withObject:anObject
withObject:anotherObject];
#pragma clang diagnostic pop
}
- (void)OUPerformSelector:(SEL)aSelector
withObject:(id)anArgument
afterDelay:(NSTimeInterval)delay
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
return [self performSelector:aSelector
withObject:anArgument
afterDelay:delay];
#pragma clang diagnostic pop
}
- (void)OUPerformSelector:(SEL)aSelector
withObject:(id)anArgument
afterDelay:(NSTimeInterval)delay
inModes:(NSArray *)modes
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
return [self performSelector:aSelector
withObject:anArgument
afterDelay:delay
inModes:modes];
#pragma clang diagnostic pop
}
- (void)OUPerformSelectorOnMainThread:(SEL)aSelector
withObject:(id)arg
waitUntilDone:(BOOL)wait
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
return [self performSelectorOnMainThread:aSelector
withObject:arg
waitUntilDone:wait];
#pragma clang diagnostic pop
}
- (void)OUPerformSelectorOnMainThread:(SEL)aSelector
withObject:(id)arg
waitUntilDone:(BOOL)wait
modes:(NSArray *)array
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
return [self performSelectorOnMainThread:aSelector
withObject:arg
waitUntilDone:wait
modes:array];
#pragma clang diagnostic pop
}
- (void)OUPerformSelector:(SEL)aSelector
onThread:(NSThread *)thread
withObject:(id)arg
waitUntilDone:(BOOL)wait
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
return [self performSelector:aSelector
onThread:thread
withObject:arg
waitUntilDone:wait
];
#pragma clang diagnostic pop
}
- (void)OUPerformSelector:(SEL)aSelector
onThread:(NSThread *)thread
withObject:(id)arg
waitUntilDone:(BOOL)wait
modes:(NSArray *)array
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
return [self performSelector:aSelector
onThread:thread
withObject:arg
waitUntilDone:wait
modes:array
];
#pragma clang diagnostic pop
}
- (void)OUPerformSelectorInBackground:(SEL)aSelector
withObject:(id)arg
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
return [self performSelectorInBackground:aSelector
withObject:arg];
#pragma clang diagnostic pop
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment