-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert
UIAlertAction
swizzling to better, more Swift-dependent method
[#142848761]
- Loading branch information
Showing
5 changed files
with
56 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,21 @@ | ||
#import "UIAlertAction+Fleet.h" | ||
#import <objc/runtime.h> | ||
|
||
@interface ObjectifiedBlock : NSObject | ||
|
||
@property (nonatomic, copy) void (^block)(UIAlertAction *); | ||
|
||
- (instancetype)initWithBlock:(void (^)(UIAlertAction *))block; | ||
|
||
@end | ||
|
||
@implementation ObjectifiedBlock | ||
|
||
- (instancetype)initWithBlock:(void (^)(UIAlertAction *))block { | ||
if (self == [super init]) { | ||
self.block = block; | ||
} | ||
|
||
return self; | ||
} | ||
|
||
@end | ||
#import <UIKit/UIKit.h> | ||
#import "FleetSwizzle.h" | ||
|
||
BOOL didSwizzleUIAlertAction = NO; | ||
unsigned int handlerAssociatedKey = 0; | ||
|
||
@interface UIAlertAction (FleetPrivate) | ||
|
||
@property (nonatomic, copy) void (^fleet_property_handler)(UIAlertAction *); | ||
|
||
@end | ||
|
||
@implementation UIAlertAction (FleetPrivate) | ||
|
||
+ (void)initialize { | ||
if (self == [UIAlertAction class]) { | ||
if (!didSwizzleUIAlertAction) { | ||
[self swizzleHandlerSettler]; | ||
[self objc_swizzleHandlerSetter]; | ||
didSwizzleUIAlertAction = YES; | ||
} | ||
} | ||
} | ||
|
||
+ (void)swizzleHandlerSettler { | ||
SEL originalSelector = NSSelectorFromString(@"setHandler:"); | ||
SEL swizzledSelector = @selector(fleet_setHandler:); | ||
|
||
Method originalMethod = class_getInstanceMethod(self, originalSelector); | ||
Method swizzledMethod = class_getInstanceMethod(self, swizzledSelector); | ||
|
||
method_exchangeImplementations(originalMethod, swizzledMethod); | ||
} | ||
|
||
- (void)fleet_setHandler:(void (^)(UIAlertAction *))handler { | ||
self.fleet_property_handler = handler; | ||
[self fleet_setHandler: handler]; | ||
} | ||
|
||
- (void (^)(UIAlertAction *))fleet_property_handler { | ||
ObjectifiedBlock *block = objc_getAssociatedObject(self, &handlerAssociatedKey); | ||
return [block block]; | ||
} | ||
|
||
- (void)setFleet_property_handler:(void (^)(UIAlertAction *))fleet_property_handler { | ||
ObjectifiedBlock *block = [[ObjectifiedBlock alloc] initWithBlock: fleet_property_handler]; | ||
objc_setAssociatedObject(self, &handlerAssociatedKey, block, OBJC_ASSOCIATION_RETAIN); | ||
+ (void)objc_swizzleHandlerSetter { | ||
memorySafeExecuteSelector(self, NSSelectorFromString(@"swizzleHandlerSetter")); | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import UIKit | ||
import ObjectiveC | ||
|
||
private var handlerAssociatedKey: UInt = 0 | ||
|
||
@objc private class ObjectifiedBlock: NSObject { | ||
var block: ((UIAlertAction) -> Void)? | ||
|
||
init(block: ((UIAlertAction) -> Void)?) { | ||
self.block = block | ||
} | ||
} | ||
|
||
extension UIAlertAction { | ||
var handler: ((UIAlertAction) -> Void)? { | ||
get { | ||
return fleet_property_handler | ||
} | ||
} | ||
|
||
@objc class func swizzleHandlerSetter() { | ||
let originalSelector = Selector(("setHandler:")) | ||
let swizzledSelector = #selector(UIAlertAction.fleet_setHandler(_:)) | ||
|
||
let originalMethod = class_getInstanceMethod(self, originalSelector) | ||
let swizzledMethod = class_getInstanceMethod(self, swizzledSelector) | ||
|
||
method_exchangeImplementations(originalMethod, swizzledMethod) | ||
} | ||
|
||
func fleet_setHandler(_ handler: ((UIAlertAction) -> Void)?) { | ||
fleet_property_handler = handler | ||
fleet_setHandler(handler) | ||
} | ||
|
||
fileprivate var fleet_property_handler: ((UIAlertAction) -> Void)? { | ||
get { | ||
let block = objc_getAssociatedObject(self, &handlerAssociatedKey) as? ObjectifiedBlock | ||
return block?.block | ||
} | ||
|
||
set { | ||
let block = ObjectifiedBlock(block: newValue) | ||
objc_setAssociatedObject(self, &handlerAssociatedKey, block, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
#import <UIKit/UIKit.h> | ||
#import "FleetObjC.h" | ||
#import "UIAlertAction+Fleet.h" | ||
|
||
FOUNDATION_EXPORT double FleetVersionNumber; | ||
FOUNDATION_EXPORT const unsigned char FleetVersionString[]; |