Unable to access the variable present in the class from the handler

import UIKit

class ViewController: UIViewController
{

    var icnNum : Int64 = 0

    let stopHandler =
        {
            (action:UIAlertAction!) -> Void in

            let num = icnNum
    }


    func showAlert( userStatus: String )
    {

        let alert = UIAlertController(title: "", message: "", preferredStyle: .alert)

        alert.title = "What you want to do?"

        alert.addAction(UIAlertAction(title: "Stop", style: .default, handler: stopHandler))

    }



}

I do not know how to access this icnNumfrom a handler. I get the following error. I know that I cannot access this variable directly, but what is the way.

Instance member "icnNum" cannot be used for type "ViewController"

+4
source share
3 answers

Define the stopHandler closure inside the function showAlert()and it should work.

class ViewController: UIViewController
{
    var icnNum : Int64 = 0

        func showAlert( userStatus: String ) {
            let stopHandler = { (action:UIAlertAction!) -> Void in
                let num = self.icnNum
            }

            let alert = UIAlertController(title: "", message: "", preferredStyle: .Alert)
            alert.title = "What you want to do?"

            alert.addAction(UIAlertAction(title: "Stop", style: .Default, handler: stopHandler))
        }   
    }
}

The compiler will force you to write self.icnNuminstead icnNum, so that it is obvious that the closure will contain a reference to self.

stopHandler , , . ViewController stopHandler, self ( ViewController).

, stopHandler

class ViewController: UIViewController {      
    var icnNum : Int64 = 0
    var stopHandler: ((action:UIAlertAction!) -> Void)?

    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        self.stopHandler = { [weak self] (action:UIAlertAction!) -> Void in
            let num = self?.icnNum
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


    func showAlert( userStatus: String )
    {
        let alert = UIAlertController(title: "", message: "", preferredStyle: .Alert)
        alert.title = "What you want to do?"

        alert.addAction(UIAlertAction(title: "Stop", style: .Default, handler: stopHandler))      
    }
}

[weak self] stopHandler. , , .

: https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/AutomaticReferenceCounting.html#//apple_ref/doc/uid/TP40014097-CH20-ID57

+5

,

 let stopHandler = {
        (icnNum: Int64 ,action:UIAlertAction!) -> Void in

        let num = icnNum
}

,

alert.addAction(UIAlertAction(title: "Stop", style: .default, handler: stopHandler(self.icnNum, UIAlertAction!)))
+2

. .

class ViewController: UIViewController {

    var icnNum : Int64 = 0

    func stopHandler(_ action: UIAlertAction) {
        let num = icnNum
    }

    func showAlert(userStatus: String) {
        let alert = UIAlertController(title: "", message: "", preferredStyle: .alert)

        alert.title = "What you want to do?"

        alert.addAction(UIAlertAction(title: "Stop", style: .default, handler: stopHandler))
    }

}

UPDATE

, , , .

func showAlert(userStatus: String) {
    let localContext = "Local information"
    // …
    alert.addAction(UIAlertAction(title: "Stop", style: .default) { action in
        if localContext == "Local information" {
           // Do Something
        }
    })
}

. , , .

func showAlert(userStatus: String) {
    // …
    alert.addAction(UIAlertAction(title: "Stop", style: .default, handler: stopHandler))
}

, , .

func stopHandler(_ action: UIAlertAction) {
    let num = icnNum
}

, , .

+1

Source: https://habr.com/ru/post/1662243/


All Articles