How do I call a function in Swift Xcode

Asked

Viewed 350 times

1

Good evening, I have a function that is attached to a button, I would like to call it directly without the need of the button, what I do?

The function...

static func goToTutorialOrTo(market: Market, from viewController: UIViewController) {

    BusinessesContainer.selectedMarket = market

    AnswersHelper.sendAccessMarketTracker()

    if UserDefaultManager.didSawTutorial() {
        let rootMarketViewController = StoryBoard.rootMarketViewController()
        viewController.present(rootMarketViewController, animated: false, completion: nil)
    } else {
        let tutorialViewController = StoryBoard.main().instantiateViewController(withIdentifier: "tutorial")
        viewController.present(tutorialViewController, animated: true, completion: nil)
    }
}

The function called by the button...

extension SupermercadosZonaViewController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        StoryBoard.goToTutorialOrTo(market: selectedMarketsToShowInTableView[indexPath.row], from: self)
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return StoryBoard.isIpad ? 175 : 110
    }
}

My attempt to call her directly

First...

StoryBoard.goToTutorialOrTo(market: Market.init(json: <#T##JSON#>)!, from: self as! UIViewController)

Second...

SupermercadosZonaViewController()

Neither I have succeeded.

  • What error do you find when calling your function? Please share the log as well.

  • when use second test>>>>>>>>>>>>>>>>>>>>>>>&#Xa attempted;Fatal error: To read an unowned Reference but the Object was already deallocated2018-05-16 16:02:51.915443-0300 Mercadapp[5068:62259] Fatal error: Attempted to read an unowned Reference but the Object was already deallocated

  • The error says that you used an object that didn’t "manage" your own memory (unowned reference), I mean, I didn’t have a strong relationship (strong) with the object in question. It seems that it has nothing to do with the shape you are presenting your screen, but rather with some object used in this routine. Tip: Search for that code block in your project for a capture list or any reference to Unmanaged.

1 answer

1

The function you want to call is the goToTutorialOrTo right? By the code you shared, this is a static function, i.e., it belongs to the class where it was defined.

To call her, you can do so:

Classe.goToTutorialOrTo(market: nil, from: nil)

Where Classe is the name of the class this function is set in. Also be sure to pass the parameters market and from.

  • Nil is not compatible with expected argument type 'Market'

  • Yes, remember to pass the correct values to the parameters market and from. The nil that I used here was just an example.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.