Search code examples
swiftavfoundationvoid

Extract a String Variable from a Void function


here is my problem :

I would like to recover the variable jan from the Launch App() function and insert it in the override above instead of "Hello there".

        override func prepare(for segue: UIStoryboardSegue, sender: Any?){

            let destVC : troisViewController = segue.destination as! troisViewController

            destVC.dataFromFirst = "Hello there"
        }

        func launchApp(decodedURL: String) -> Void {

            if presentedViewController != nil{
            return
            }

        let jan: String = "\(decodedURL)"
        print(jan)


        self.performSegue(withIdentifier: "troissegue", sender: self)
    }

The problem is the decoded URL is a barcode obtained by using the camera of my phone, a solution of type : destVC.dataFromFirst = launchApp() does not work...

Does Anyone has a similar issue ?

Thank you in advance


Solution

  • A simple solution is to pass the string as sender parameter

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {    
        if segue.identifier == "troissegue" {
            let destVC = segue.destination as! troisViewController
            destVC.dataFromFirst = sender as! String
        }
    }
    
    func launchApp(decodedURL: String) -> Void {
    
        if presentedViewController != nil { return }
        self.performSegue(withIdentifier: "troissegue", sender: decodedURL)
    }