-2

I am currently modifying this tutorial from https://www.simplifiedios.net/swift-php-mysql-tutorial/ . The purpose of the program is to close a ViewController if the correct message is recieved and to display a UIAltertAction if an invalid message is recieved. Could there be something that i am missing that "notChangingVariable" is never changing?

    ...IBACTION...
    var notChangingVariable: Int

    notChangingVariable = 0
    ...othervariablesdeclared...
    let task = URLSession.shared.dataTask(with: request as URLRequest){
        data, response, error in
        if(error != nil){
            return;
        }

        //parsing the response
        do{
            //converting resonse to NSDictionary
            let myJSON =  try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
            //parsing the json
            if let parseJSON = myJSON{
                var msg : String!
                //getting the json response
                msg = parseJSON["message"] as! String?
                if(msg == "SameTeam"){
                    print(msg)
                    notChangingVariable  = 1
                    print(notChangingVariable)
                }
            }
        }catch{
            print("error32 is \(error)")
        }
    }
    task.resume()
    print(notChangingVariable) //IS 0

    ...FURTHER USE notChangingVariable...
  • 1
    This has been asked and answered a lot already. Please have a look at this example - if you don't like this one, there's plenty existing others. – Eric Aya Dec 01 '16 at 13:46

2 Answers2

0

The line

notChangingVariable  = 1 

is inside a completion handler. It will be called, and the var will be changed, but it will happen after the line

print(notChangingVariable) //IS 0
Witterquick
  • 6,048
  • 3
  • 26
  • 50
  • So at what point/how can i use it after it officially has been changed? – HONGNAT Dec 01 '16 at 13:49
  • You can call a function from the completion handler in which you can use your var – Witterquick Dec 01 '16 at 13:52
  • Ok i have done that but now another issue arises where i get a "terminating with uncaught exception of type NSException" when i try to miss a view controller in the closure or Show a action controller – HONGNAT Dec 02 '16 at 03:57
0

you are printing it before this part execution:

do{//converting resonse to NSDictionary
            let myJSON =  try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
            //parsing the json
            if let parseJSON = myJSON{
                var msg : String!
                //getting the json response
                msg = parseJSON["message"] as! String?
                if(msg == "SameTeam"){
                    print(msg)
                    notChangingVariable  = 1
                    print(notChangingVariable)
                }
            }
        }

it will never change in this scenario.

Daniel
  • 20,420
  • 10
  • 92
  • 149
Sagar Snehi
  • 398
  • 3
  • 13