1

I have this code

func presentationlum() { 
    let request = NSMutableURLRequest(URL: NSURL(string: "http://raspberrypi.local/etatlum.php")!)

    let Session = NSURLSession.sharedSession()
    request.HTTPMethod = "GET"

    var JsonDict : NSArray = []
    let dem = Session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
        do {
            if let _JsonDict = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions() )as? NSArray
            {
                JsonDict = _JsonDict
                if JsonDict[0] as! String == "0"{
                    Dark1.hidden=true
                } else {
                    Dark1.hidden=false
                }
            } else {
                print("Cannot parse JSON answer")
            }
        } catch {
            print("An Error as occurred : \(error)")
        }
        print(JsonDict)
    })
    dem.resume()
}

I want to get the response of my server and use it to hide the UIButton Dark1.

But it doesn't work how can I fix it ?

Simon McLoughlin
  • 8,293
  • 5
  • 32
  • 56
S.P98
  • 11
  • 1
  • 3
    What doesn't work? In which case (do/if/else/catch) do your app goes? Avoid naming var starting with an uppercase. – Larme Jul 05 '16 at 14:29
  • Why do you declare an empty NSArray as `JsonDict` and then you make a new one with `if let` and then again you copy this one to the first one?! Also, as Larme told you, variables and properties should be lowerCamelCase. And don't force unwrap your optionals. You should also use `self.Dark1` instead of `Dark1` in the closure. – Eric Aya Jul 05 '16 at 14:35
  • I have try to use self.Dark1 but then I have a fatal error – S.P98 Jul 05 '16 at 14:42
  • It probably means that Dark1 is not properly hooked to the button in Interface Builder. See explanations here: http://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu – Eric Aya Jul 05 '16 at 14:46
  • i will give u a big advice. use afnetworkingit is much more easier with u – Mohamad Bachir Sidani Jul 05 '16 at 14:47
  • 1
    @BashirSidani Nonsense. Using AFNetworking (or its Swift equivalent, Alamofire) has nothing to do with the issues at hand, and wouldn't help OP a bit. – Eric Aya Jul 05 '16 at 14:55
  • Where is this method being called? Is it in a `viewWillAppear`? It could be you need to move it to `viewDidLoad`. – Mario Segura Jul 05 '16 at 15:44

0 Answers0