Working with Objective-C blocks with Swift

后端 未结 3 1538
北恋
北恋 2020-12-03 18:03

I\'m having trouble using the Objective-C Firebase framework in a new Swift project. I\'m coming from mostly a C# background so the Swift closure syntax isn\'t that clear y

相关标签:
3条回答
  • 2020-12-03 18:16

    Here's the Swift equivalent:

    f.observeEventType(FEventTypeValue, withBlock: {
        snapshot in
        println("\(snapshot.name) -> \(snapshot.value)")
    })
    

    The key here is the in keyword to assign arguments to the closure to variables

    0 讨论(0)
  • 2020-12-03 18:28

    To throw in implied names and tail closures, you can use:

    f.observeEventType(FEventTypeValue) {
        println("\($0.name) -> \($0.value)")
    }
    
    0 讨论(0)
  • 2020-12-03 18:39

    Swift blocks are interchangeable with Objective-C blocks, so it ought to be something like:

    f.observeEventType(FEventTypeValue, withBlock: { 
        snapshot in 
        println("\(snapshot.name) -> \(snapshot.value)")
    })
    
    0 讨论(0)
提交回复
热议问题