如何在Xcode 6.1的Swift中使用NSURL?

磁铁

我究竟做错了什么?我正在尝试使用api,但是首先我需要学习快速编写http东西。

我在操场上使用此代码:

// Playground - noun: a place where people can play

// import Cocoa - this is commented out due to "No such module 'Cocoa'"
import XCPlayground

let url = NSURL(string: "http://stackoverflow.com")
let request = NSURLRequest(URL: url)

var waiting = true

NSURLConnection.sendAsynchronousRequest(request, queue:NSOperationQueue.currentQueue() {
    response, maybeData, error in
    waiting = false
    if let data = maybeData {
        let contents = NSString(data:data, encoding:NSUTF8StringEncoding)
        println(contents)
    } else {
        println(error.localizedDescription)
    }
    }

    while(waiting) {
    NSRunLoop.currentRunLoop().runMode(NSDefaultRunLoopMode, beforeDate: NSDate())
    usleep(10)
}

并在控制台中得到以下错误:

Playground execution failed: <EXPR>:12:11: error: use of unresolved identifier 'NSURL'
let url = NSURL(string: "http://www.stackoverflow.com")
          ^
<EXPR>:14:12: error: use of unresolved identifier 'NSURLSession'
let task = NSURLSession.sharedSession().dataTaskWithURL(url) {(data, response, error) in
           ^
<EXPR>:15:13: error: use of unresolved identifier 'NSString'
    println(NSString(data: data, encoding: NSUTF8StringEncoding))
            ^
<EXPR>:15:44: error: use of unresolved identifier 'NSUTF8StringEncoding'
    println(NSString(data: data, encoding: NSUTF8StringEncoding))
弗拉基米尔

您需要导入Foundation框架以使这些类型可用。因此,将以下导入行添加到您的游乐场:

import Foundation

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Xcode 6和Storyboard中使用NSToolBar?

如何在XCode6中使用NSMutableRequest

如何在xcode 6的Today扩展中使用UITextField?

如何在XCode6中使用StringByAppendingFormat

Swift Xcode 6:如何在一个语句中使用多个函数?

如何在Swift Xcode 6中更改按钮文本?

如何在Swift 4.1中使用NSURL:setResourceValues()?

Java到Swift-如何在Xcode 6中使用math.pow和math.exp

如何在Xcode 6中使用XIB创建iOS项目?

如何在Xcode 6 Interface Builder中使用模板渲染模式?

如何在Xcode 6的iOS中使用Storyboard执行self.view.frame.size.width?

如何在Xcode 6中使用mapKit在点之间画线

如何在Angular 6中使用take(1)?

如何在xcode 6中使UI文本字段看起来像这样?

如何在Xcode 6调试器中查看Swift“ let”常量的值

如何在Xcode 6 iOS应用中实现main.swift?

如何在Xcode 6(Swift)中创建没有故事板的项目?

如何在 Quarkus 中使用 Hibernate 6

在Xcode 6中使用自定义字体-Swift

在Xcode 6中使用Swift的AdMob横幅

如何在NSURL中使用特殊字符?

如何在Xcode 6中隐藏按钮?

如何在Xcode 6中创建IPA?

如何使用Swift在Xcode 6的numberOfItemsSection中传递多个值?

如何使用Swift在Xcode 6中执行自动搜索?

在Xcode 6中,如何使用硬件键盘,但如何在iOS模拟器中显示软件键盘

如何在XCode 8 Playgrounds中使用Swift 2.3?

如何在Xcode OS X Swift Playground中使用GLKit?

如何在Xcode,Swift中使用按钮创建相同的对象