내가 뭘 잘못하고 있니? 나는 API를 사용하려고 시도하지만, 우선 신속하게 HTTP를 처리하는 법을 배워야합니다.Xcode 6.1에서 Swift에서 NSURL을 어떻게 사용합니까?
// 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))
NSUrlConnection과 NSUrlConnectionDataDelegate를 비동기 호출에 사용하는 것이 좋습니다. 테스트 프로젝트를하려고 Playgroud에서 조금 어려울 수도 있기 때문에, 그것을 테스트하고 실제 환경에서 사용할 준비가되어 있습니다. –