-4

I am trying to convert my Objective C source code to swift, I have searched alot to change syntax for Blocks.

Here is my Objective C code that I want to switch to Swift :

class.h file :

@property (nonatomic, copy) void (^tapBlock)(CGFlagsCell *);

+ (NSString *)cellIdentifier;

Class.m file :

self.tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapCell)];

- (void)didTapCell {
    self.tapBlock(self);
}
Dániel Nagy
  • 11,815
  • 9
  • 50
  • 58
Anand Gautam
  • 2,541
  • 3
  • 34
  • 70
  • I have converted the class method as class func cellIdentifier() -> String { return "yourValue" } please help me for the rest. Thanks! – Anand Gautam Apr 26 '15 at 12:16
  • I don't get what you want exactly . – BoilingLime Apr 26 '15 at 12:31
  • @ BoilingLime I want to convert my above objective C code to Swift for Blocks. My Objective C code is : @property (nonatomic, copy) void (^tapBlock)(CGFlagsCell *); – Anand Gautam Apr 26 '15 at 12:36
  • If anyone can't help me, so why you guys are doing down vote.... If you are thinking that my question is not good for StackOverFlow.. then tell me the reason.. – Anand Gautam Apr 26 '15 at 12:42

2 Answers2

0

In Swift, blocks, functions and closures are the same thing, they have the same signature and are interchangeable.

So this would give you something like this

var  tapBlock: CGFlagsCell -> Void

You can add parenthesis around the parameter (optional if there is only one, recommended if there are multiple input parameters) and around the return type (optional):

var tapBlock: (CGFlagsCell) -> (Void)
AliSoftware
  • 32,623
  • 6
  • 82
  • 77
  • Thanks alot @AliSoftware for your help... It's working fine now, Can you please give me some idea for UITapGestureRecognizer if possible, As this is my custom UITableViewCell. – Anand Gautam Apr 26 '15 at 12:50
  • Thanks again @AliSoftware... But I don't know why down vote, anyway this will help to other programmers.. :) – Anand Gautam Apr 26 '15 at 12:55
0

Define a closure a variable of your class :

var tapBlock: (parameterTypes) -> (returnType)

Swift Closures are self-contained blocks of functionality that can be passed around and used in your code. Closures in Swift are similar to blocks Objective-C

BoilingLime
  • 2,207
  • 3
  • 22
  • 37