How to use NSURL in Swift in Xcode 6.1?

What am I doing wrong? I'm trying to use api, but first I need to learn how to do HTTP things in fast.

I use this code on the playground:

// 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) } 

and get these errors in the console:

 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)) 
+5
source share
1 answer

You need to import the Foundation framework so that these types are available. So add the following import line to your site:

 import Foundation 
+9
source

Source: https://habr.com/ru/post/1206802/


All Articles