Skip to content

Instantly share code, notes, and snippets.

@nerdo
Forked from cprovatas/BlockBasedSelector.h
Created February 9, 2018 23:24
Show Gist options
  • Select an option

  • Save nerdo/e0cdc89aad27fbb43d98b21e8ef80c65 to your computer and use it in GitHub Desktop.

Select an option

Save nerdo/e0cdc89aad27fbb43d98b21e8ef80c65 to your computer and use it in GitHub Desktop.

Revisions

  1. @cprovatas cprovatas revised this gist Jan 30, 2018. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions BlockBasedSelector.swift
    Original file line number Diff line number Diff line change
    @@ -7,6 +7,7 @@
    //

    import Foundation
    import UIKit
    // swiftlint:disable identifier_name
    func Selector(_ block: @escaping () -> Void) -> Selector {
    let selector = NSSelectorFromString("\(CACurrentMediaTime())")
  2. @cprovatas cprovatas revised this gist Dec 7, 2017. 3 changed files with 31 additions and 7 deletions.
    5 changes: 3 additions & 2 deletions BlockBasedSelector.h
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,8 @@

    @end

    typedef void (^OBJCBlock)(id foo);
    typedef void (^OBJCBlock)(id PPSelector);
    typedef void (^OBJCBlockWithSender)(id PPSelector, id sender);

    void class_addMethodWithBlock(Class class, SEL newSelector, OBJCBlock block);

    void class_addMethodWithBlockAndSender(Class class, SEL newSelector, OBJCBlockWithSender block);
    8 changes: 7 additions & 1 deletion BlockBasedSelector.m
    Original file line number Diff line number Diff line change
    @@ -12,9 +12,15 @@ @implementation BlockBasedSelector
    @end

    void class_addMethodWithBlock(Class class, SEL newSelector, OBJCBlock block)
    {
    {
    IMP newImplementation = imp_implementationWithBlock(block);
    Method method = class_getInstanceMethod(class, newSelector);
    class_addMethod(class, newSelector, newImplementation, method_getTypeEncoding(method));
    }

    void class_addMethodWithBlockAndSender(Class class, SEL newSelector, OBJCBlockWithSender block)
    {
    IMP newImplementation = imp_implementationWithBlock(block);
    Method method = class_getInstanceMethod(class, newSelector);
    class_addMethod(class, newSelector, newImplementation, method_getTypeEncoding(method));
    }
    25 changes: 21 additions & 4 deletions BlockBasedSelector.swift
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,28 @@
    //
    // BlockBasedSelector.swift
    // Parking
    //
    // Created by Charlton Provatas on 11/9/17.
    // Copyright © 2017 Passport Parking. All rights reserved.
    //

    import Foundation
    // swiftlint:disable identifier_name
    func Selector(_ block: @escaping () -> Void) -> Selector {
    let selector = NSSelectorFromString("\(CACurrentMediaTime())")
    class_addMethodWithBlock(_Selector.self, selector) { (_) in block() }
    class_addMethodWithBlock(PPSelector.self, selector) { _ in block() }
    return selector
    }

    let Selector = _Selector.shared
    @objc class _Selector: NSObject {
    static let shared = _Selector()
    /// used w/ callback if you need to get sender argument
    func Selector(_ block: @escaping (Any?) -> Void) -> Selector {
    let selector = NSSelectorFromString("\(CACurrentMediaTime())")
    class_addMethodWithBlockAndSender(PPSelector.self, selector) { (_, sender) in block(sender) }
    return selector
    }

    // swiftlint:disable identifier_name
    let Selector = PPSelector.shared
    @objc class PPSelector: NSObject {
    static let shared = PPSelector()
    }
  3. @cprovatas cprovatas revised this gist Nov 2, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion BlockBasedSelector.h
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    //
    // RunTimeWrapper.h
    // BlockBasedSelector.h
    //
    // Created by Charlton Provatas on 11/2/17.
    // Copyright © 2017 CharltonProvatas. All rights reserved.
  4. @cprovatas cprovatas revised this gist Nov 2, 2017. No changes.
  5. @cprovatas cprovatas revised this gist Nov 2, 2017. No changes.
  6. @cprovatas cprovatas revised this gist Nov 2, 2017. 3 changed files with 3 additions and 0 deletions.
    1 change: 1 addition & 0 deletions BlockBasedSelector.h
    Original file line number Diff line number Diff line change
    @@ -14,3 +14,4 @@
    typedef void (^OBJCBlock)(id foo);

    void class_addMethodWithBlock(Class class, SEL newSelector, OBJCBlock block);

    1 change: 1 addition & 0 deletions BlockBasedSelector.m
    Original file line number Diff line number Diff line change
    @@ -17,3 +17,4 @@ void class_addMethodWithBlock(Class class, SEL newSelector, OBJCBlock block)
    Method method = class_getInstanceMethod(class, newSelector);
    class_addMethod(class, newSelector, newImplementation, method_getTypeEncoding(method));
    }

    1 change: 1 addition & 0 deletions BlockBasedSelector.swift
    Original file line number Diff line number Diff line change
    @@ -8,3 +8,4 @@ let Selector = _Selector.shared
    @objc class _Selector: NSObject {
    static let shared = _Selector()
    }

  7. @cprovatas cprovatas created this gist Nov 2, 2017.
    16 changes: 16 additions & 0 deletions BlockBasedSelector.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    //
    // RunTimeWrapper.h
    //
    // Created by Charlton Provatas on 11/2/17.
    // Copyright © 2017 CharltonProvatas. All rights reserved.
    //

    #import <Foundation/Foundation.h>

    @interface BlockBasedSelector : NSObject

    @end

    typedef void (^OBJCBlock)(id foo);

    void class_addMethodWithBlock(Class class, SEL newSelector, OBJCBlock block);
    19 changes: 19 additions & 0 deletions BlockBasedSelector.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    //
    // BlockBasedSelector.m
    //
    // Created by Charlton Provatas on 11/2/17.
    // Copyright © 2017 CharltonProvatas. All rights reserved.
    //

    #import "BlockBasedSelector.h"
    #import <objc/runtime.h>

    @implementation BlockBasedSelector
    @end

    void class_addMethodWithBlock(Class class, SEL newSelector, OBJCBlock block)
    {
    IMP newImplementation = imp_implementationWithBlock(block);
    Method method = class_getInstanceMethod(class, newSelector);
    class_addMethod(class, newSelector, newImplementation, method_getTypeEncoding(method));
    }
    10 changes: 10 additions & 0 deletions BlockBasedSelector.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    func Selector(_ block: @escaping () -> Void) -> Selector {
    let selector = NSSelectorFromString("\(CACurrentMediaTime())")
    class_addMethodWithBlock(_Selector.self, selector) { (_) in block() }
    return selector
    }

    let Selector = _Selector.shared
    @objc class _Selector: NSObject {
    static let shared = _Selector()
    }