lundi 7 novembre 2016

If statement outputting both arguments instead of either?

I have the following code but instead of outputting either of the statements; both statements are output:

response = Optional( { URL: pttp://10.0.1.147/setpins?D1=0 } { status code: 200, headers { "Access-Control-Allow-Origin" = ""; Connection = close; "Content-Length" = 7; "Content-Type" = "text/plain"; } })

response = Optional( { URL: pttp://10.0.1.147/setpins?D2=1023 } { status code: 200, headers { "Access-Control-Allow-Origin" = ""; Connection = close; "Content-Length" = 7; "Content-Type" = "text/plain";

The switch changes and the labels prints "On When I press the switch again (To Off) I get the reverse of the above output:

response = Optional( { URL: pttp://10.0.1.147/setpins?D2=1023 } { status code: 200, headers { "Access-Control-Allow-Origin" = ""; Connection = close; "Content-Length" = 7; "Content-Type" = "text/plain"; } })

response = Optional( { URL: pttp://10.0.1.147/setpins?D1=0 } { status code: 200, headers { "Access-Control-Allow-Origin" = "*"; Connection = close; "Content-Length" = 7; "Content-Type" = "text/plain"; } })

Again, the label works but both outputs?? What am I missing here?

} })

@IBOutlet weak var mySwitch: UISwitch!
@IBOutlet weak var switchState: UILabel!
@IBAction func buttonClicked(_ sender: Any)
{

    if mySwitch.isOn {
        switchState.text = "OFF"
        mySwitch.setOn(false, animated:true)

        let string1 = "http://"
        let string2 = "10.0.1.147"
        let string3 = "/setpins?D1=0"


        let myUrl = URL(string: string1 + string2 + string3);

        var request = URLRequest(url:myUrl!)

        request.httpMethod = "POST"// Compose a query string

        let postString = "";

        request.httpBody = postString.data(using: String.Encoding.utf8);

        let task = URLSession.shared.dataTask(with: request)
        { (data: Data?, response: URLResponse?, error: Error?) in

            if error != nil
            {
                print("error=\(error)")
                return
            }

            // You can print out response object
            print("response = \(response)")

        }
        task.resume()


    } else {
        switchState.text = "On"
        mySwitch.setOn(true, animated:true)
        let string1 = "http://"
        let string2 = "10.0.1.147"
        let string3 = "/setpins?D2=1023"


        let myUrl = URL(string: string1 + string2 + string3);

        var request = URLRequest(url:myUrl!)

        request.httpMethod = "POST"// Compose a query string

        let postString = "";

        request.httpBody = postString.data(using: String.Encoding.utf8);

        let task = URLSession.shared.dataTask(with: request)
        { (data: Data?, response: URLResponse?, error: Error?) in

            if error != nil
            {
                print("error=\(error)")
                return
            }

            // You can print out response object
            print("response = \(response)")

        }
        task.resume()
        }

}

Aucun commentaire:

Enregistrer un commentaire