I'm updating a legacy app and I want to add a nullable, assignable closure of type (() -> ())? to my header file. How do I do this in Objective-C?
            Asked
            
        
        
            Active
            
        
            Viewed 173 times
        
    0
            
            
         
    
    
        JAL
        
- 41,701
- 23
- 172
- 300
 
    
    
        Zack Shapiro
        
- 6,648
- 17
- 83
- 151
- 
                    A closure is called a `block` in Objective-C. See there: http://fuckingblocksyntax.com If it's a method closure see the one there: https://developer.apple.com/documentation/uikit/uiviewcontroller/1621380-presentviewcontroller?language=objc – Larme Oct 27 '18 at 17:18
- 
                    Possible duplicate of [Block Declaration Syntax List](https://stackoverflow.com/questions/9201514/block-declaration-syntax-list) – JAL Oct 27 '18 at 17:50
1 Answers
0
            
            
        In my .h file, this is what I was going for:
@property (nullable, copy) void (^myNullableClosure) (void);
Then I can assign in my Swift code as:
let vc = ViewController()
vc.myNullableClosure = {
    print("something")
}
then later in the ViewController, I call myNullableClosure?()
 
    
    
        Zack Shapiro
        
- 6,648
- 17
- 83
- 151