简体   繁体   中英

How to implement function to pointer in swift

 #import "ViewController.h"

    @interface ViewController ()

    //Declare block as property

    @property (nonatomic, strong) void (^dataBlock)(BOOL success);

    @end

    @implementation ViewController

    - (void) myMethod1:(void (^)(BOOL success))response {

        //Here data block holds the reference to response block

        _dataBlock = response;    
    }

    - (void) myMethod2 {

        //Check for _dataBlock and invoke it.

        if (_dataBlock) {    
            _dataBlock(YES);   
        }    
    }

   - (IBAction) buttonClick {

     //Call for myMethod1 and call back block is invoked in myMethod2

        [self myMethod1:^(BOOL success) { 

            if (success) {   
                NSLog(@"Im Done");   
            }    

        }];
   }
   @end

Above sample is my code in Objective-C

  • Callback Block of "myMethod1"(response) is having reference/stored to "dataBlock" Property.
  • then invoke "dataBlock" from "myMethod2".
  • since "datablock" have reference to"myMethod1" block named "response", i'll be getting call back in "myMethod1", please look at the code snippet (similar to function to pointer).
  • same thing i want to implement in swift. I have tried implementing this in swift using closures, but not getting it.

No, there is not, unless it's on a Jailbroken device.

Apple does not let 3rd party apps alter the core behavior of the phone.

Now you could put the phone in a Faraday cage and put antennas on the inside and outside, and disconnect them when you wanted to block calls.

Actually you sort of can, but not really programmatically from iOS. If the BLE device implements the HID profile then you can simulate a double click on the lock button which would dismiss the call. I have done that, but it is a bit of a clunky solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM