0

I'm trying do an inside app notification when the network connexion is either slow or disconnected.

If the user wants to reload I want to call the function passed in parameter (callback) but Xcode won't let me do that :

Argument of '#selector' cannot refer to parameter 'callback'

Here's my code :

func listenForFirebaseConnection(callback: @escaping () -> Void) {
    FireDataUtil().getBase().child(".info/connected").observeSingleEvent(of: .value, with: { (snapshot) in
            if snapshot.exists() {
                let isConnected = snapshot.value as! Bool?
                if isConnected != nil && isConnected == false {

                    let view = MessageView.viewFromNib(layout: .MessageView)
                    view.button?.isHidden = false

                    // Theme message elements with the warning style.
                    view.configureTheme(.error)

                    // Add a drop shadow.
                    view.configureDropShadow()

                    // Set message title, body, and icon. Here, we're overriding the default warning
                    // image with an emoji character.
                    view.configureContent(title: "Network Issue", body: "It looks like your network connection is either slow or disconnected")
                    view.button?.setTitle("Reload", for: .normal)

                    view.button?.addTarget(self, action: #selector(callback), for: .touchUpInside)
                    var config = SwiftMessages.Config()

                    config.presentationContext = .window(windowLevel: UIWindowLevelStatusBar)
                    config.presentationStyle = .bottom
                    config.duration = .seconds(seconds: 5)

                    // Show the message.
                    SwiftMessages.show(config: config, view: view)
                }
            }
        })
}
  • you cannot call block methods or closure like this. Either declare a property of block or closure to call in the way you tried. – Kamal Bhardwaj Feb 22 '17 at 10:53
  • I've just tried to do something like in this anwser : http://stackoverflow.com/a/36983811/6303970 so I can do : let myAction = Action { callback() } and #selector(myAction.action) but I got " unrecognized selector sent to instance " – victorsarda Feb 22 '17 at 11:11
  • your code and the post you are following have different code, you are calling action inside function which is not possible. you can call methods in selectors which are either declare in class or global but not of inside method. – Kamal Bhardwaj Feb 22 '17 at 11:22

1 Answers1

0

try like this :

var objCallback:() -> Void)?

func listenForFirebaseConnection(callback: @escaping () -> Void) {
FireDataUtil().getBase().child(".info/connected").observeSingleEvent(of: .value, with: { (snapshot) in
        if snapshot.exists() {
            let isConnected = snapshot.value as! Bool?
            if isConnected != nil && isConnected == false {

                self.objCallback = callback
                let view = MessageView.viewFromNib(layout: .MessageView)
                view.button?.isHidden = false

                // Theme message elements with the warning style.
                view.configureTheme(.error)

                // Add a drop shadow.
                view.configureDropShadow()

                // Set message title, body, and icon. Here, we're overriding the default warning
                // image with an emoji character.
                view.configureContent(title: "Network Issue", body: "It looks like your network connection is either slow or disconnected")
                view.button?.setTitle("Reload", for: .normal)

                view.button?.addTarget(self, action: #selector(objCallback), for: .touchUpInside)
                var config = SwiftMessages.Config()

                config.presentationContext = .window(windowLevel: UIWindowLevelStatusBar)
                config.presentationStyle = .bottom
                config.duration = .seconds(seconds: 5)

                // Show the message.
                SwiftMessages.show(config: config, view: view)
            }
        }
    })

}

this may helps you

Kamal Bhardwaj
  • 948
  • 1
  • 10
  • 25