Store a swift closure as a property on an objective-c class where the block is declared using a typedef

shmim

OMG, for the life of me I can't get this to work.

  • I've got a typdef in objective-c that looks like this:
typedef void (^StringBlock)(NSString * string);
  • I've got an objective-c class that has a property that allows you to store your own block of StringBlock type. That property is declared in objective-c like this:
@property (nonatomic, copy) StringBlock onTextSubmitBlock;
  • Assigning a block to it in objective-c looks like this:
input.onTextSubmitBlock = ^(NSString * text) {

};
  • I want to do the same thing from within a Swift class! The closest I've come to having something that works is this:
input!.onTextSubmitBlock = {(StringBlock) in

}

That compiles, but I have no access to the argument I need ((NSString * text) in objective-c...)

I'm sure that once I get used to Swift this will be obvious, but what am I missing?

Ben Kane

You are pretty close. You should be able to use it like this:

input!.onTextSubmitBlock = { text in
    println(text)
}

Swift will infer that text is an NSString from the declaration. Thera are a couple alternative ways you could declare this as well. Like this:

input!.onTextSubmitBlock = {
    println($0) // $0 is text
}

And this:

input!.onTextSubmitBlock = { (text: NSString) -> () in
    println(text)
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Mutable parameters on swift closure using Objective-c typedef

From Dev

Converting objective-c block to Swift closure

From Dev

Calling Swift from Objective C, closure to block?

From Dev

Convert Objective-C block to Swift closure

From Dev

Objective-C Block to Swift Closure

From Dev

Using Swift closure with Objective-C framework

From Dev

Calling objective-C typedef block from swift

From Dev

Calling objective-C typedef block from swift

From Dev

Swift : Define a closure compatible with Objective-C block

From Dev

Converting Objective-C Block into Closure the Swift way

From Dev

Difference between block (Objective-C) and closure (Swift) in iOS

From Dev

Swift closure crashes when called as Objective-C block

From Dev

Convert Swift 2 closure to Objective-C block

From Dev

How to get Objective-C block input parameter in Swift Closure

From Dev

Converting Objective-C Block into Closure the Swift way

From Dev

How to get Objective-C block input parameter in Swift Closure

From Dev

converting an Objective-C block variable to a Swift closure variable?

From Dev

How to convert an Objective-C Block into a Swift Closure?

From Dev

Swift: Trouble Using Enums Declared in Objective-C, In Swift

From Dev

Error using Objective C typedef enum in Swift 3

From Dev

React Native: Swift Module store objective C block for callback by delegate

From Dev

Objective C to Java: converting a typedef block

From Dev

Objective C to Java: converting a typedef block

From Dev

Swift class using Objective-C class using Swift class

From Dev

Store a block object in objective C

From Dev

Pass a swift closure to an objective-C function which takes a block as parameter

From Dev

Using a generic Swift class in Objective-C

From Dev

Using Swift class inside Objective-C

From Dev

Objective C test class using Swift code

Related Related

  1. 1

    Mutable parameters on swift closure using Objective-c typedef

  2. 2

    Converting objective-c block to Swift closure

  3. 3

    Calling Swift from Objective C, closure to block?

  4. 4

    Convert Objective-C block to Swift closure

  5. 5

    Objective-C Block to Swift Closure

  6. 6

    Using Swift closure with Objective-C framework

  7. 7

    Calling objective-C typedef block from swift

  8. 8

    Calling objective-C typedef block from swift

  9. 9

    Swift : Define a closure compatible with Objective-C block

  10. 10

    Converting Objective-C Block into Closure the Swift way

  11. 11

    Difference between block (Objective-C) and closure (Swift) in iOS

  12. 12

    Swift closure crashes when called as Objective-C block

  13. 13

    Convert Swift 2 closure to Objective-C block

  14. 14

    How to get Objective-C block input parameter in Swift Closure

  15. 15

    Converting Objective-C Block into Closure the Swift way

  16. 16

    How to get Objective-C block input parameter in Swift Closure

  17. 17

    converting an Objective-C block variable to a Swift closure variable?

  18. 18

    How to convert an Objective-C Block into a Swift Closure?

  19. 19

    Swift: Trouble Using Enums Declared in Objective-C, In Swift

  20. 20

    Error using Objective C typedef enum in Swift 3

  21. 21

    React Native: Swift Module store objective C block for callback by delegate

  22. 22

    Objective C to Java: converting a typedef block

  23. 23

    Objective C to Java: converting a typedef block

  24. 24

    Swift class using Objective-C class using Swift class

  25. 25

    Store a block object in objective C

  26. 26

    Pass a swift closure to an objective-C function which takes a block as parameter

  27. 27

    Using a generic Swift class in Objective-C

  28. 28

    Using Swift class inside Objective-C

  29. 29

    Objective C test class using Swift code

HotTag

Archive