简单地要求在斯威夫特的网址 [英] Simply request an url in Swift

查看:142
本文介绍了简单地要求在斯威夫特的网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经运行的点亮LE​​D的输入网址(... /?LEDON),并键入(... /?ledOff)时将其关闭时对我的Arduino的一个非常基本的Web服务器。

现在我已经找到了如何这两种状态用一个非常简单的寻找目标C code从本网站控制<一个视频href=\"http://www.raywenderlich.com/15932/electronics-for-iphone-developers-tutorial-control-a-led-from-your-iphone\"相对=nofollow>这里:

   - (IBAction为)开关pressed:(ID)发送{
    UISwitch *精选完美男=(UISwitch *)寄件人;
    如果(theSwitch.isOn){
        NSURL * URL = [NSURL URLWithString:@?... / LEDON];
        *的NSURLRequest REQ = [的NSURLRequest requestWithURL:URL]
        [myWebView的loadRequest:REQ]
    }
}

于是,我就这样code转换成快捷,但有一个错误,在创建 NSURL 变量,我得到类型的变量 NSURL?


目前雨燕的尝试是这样的:

  VAR URL = NSURL(串:... / buttonOn)
VAR reqest =的NSURLRequest(网址:NSURL(URL))


解决方案

我建议用 NSURLSession 调用替换Web视图。这,顺便说一句,无需建的NSURLRequest 了。

因此​​:

  @IBAction FUNC开关pressed(发件人:UISwitch){
    如果sender.on {
        让URL = NSURL(串:... / LEDON)!
        让任务= NSURLSession.sharedSession()。dataTaskWithURL(URL){数据,回应,误差
            //解析`data`或检查`这里error`,例如
            //
            //如果错误!= {为零
            //的println(\\(错误))
            //}
        }
        task.resume()
    }
}

或者,如果你需要更新UI报告错误,请确保派遣了回主队列:

  @IBAction FUNC开关pressed(发件人:UISwitch){
    如果sender.on {
        让URL = NSURL(串:... / LEDON)!
        让任务= NSURLSession.sharedSession()。dataTaskWithURL(URL){数据,回应,误差
            如果错误!= {为零
                dispatch_async(dispatch_get_main_queue()){
                    //更新UI报告错误这里
                }
            }
        }
        task.resume()
    }
}

I have running a very basic web server on my arduino which turns the led on when typing in the url (.../?ledOn) and turning it off when typing in (.../?ledOff).

Now i have found a video on how to control these two states with a very simple looking objective c code from this website here:

- (IBAction)switchPressed:(id)sender { 
    UISwitch *theSwitch = (UISwitch *) sender; 
    if (theSwitch.isOn) { 
        NSURL *url = [NSURL URLWithString:@".../?ledOn"]; 
        NSURLRequest *req = [NSURLRequest requestWithURL:url]; 
        [myWebView loadRequest:req]
    }
}

So i tried to convert this code into swift, but there is an error, while creating the NSURL variable, I get a variable of type NSURL?


The current Swift attempt looks like:

var url = NSURL(string: ".../?buttonOn") 
var reqest = NSURLRequest(URL: NSURL(url))

解决方案

I'd suggest replacing web view with NSURLSession call. This, by the way, eliminates the need to build the NSURLRequest, too.

Thus:

@IBAction func switchPressed(sender: UISwitch) {
    if sender.on {
        let url = NSURL(string: ".../?ledOn")!
        let task = NSURLSession.sharedSession().dataTaskWithURL(url) { data, response, error in
            // parse `data` or examine `error` here, e.g.
            //
            // if error != nil {
            //     println("\(error)")
            // }
        }
        task.resume()
    }
}

Or, if you need to update the UI reporting the error, make sure to dispatch that back to the main queue:

@IBAction func switchPressed(sender: UISwitch) {
    if sender.on {
        let url = NSURL(string: ".../?ledOn")!
        let task = NSURLSession.sharedSession().dataTaskWithURL(url) { data, response, error in
            if error != nil {
                dispatch_async(dispatch_get_main_queue()) {
                    //update UI reporting error here
                }
            }
        }
        task.resume()
    }
}

这篇关于简单地要求在斯威夫特的网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆