2014-11-13 1 views
5

내가 뭘 잘못하고 있니? 나는 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)) 
+0

NSUrlConnection과 NSUrlConnectionDataDelegate를 비동기 호출에 사용하는 것이 좋습니다. 테스트 프로젝트를하려고 Playgroud에서 조금 어려울 수도 있기 때문에, 그것을 테스트하고 실제 환경에서 사용할 준비가되어 있습니다. –

답변

9

을 당신은 그 종류를 사용할 수 있도록 재단 프레임 워크를 가져와야합니다. 따라서 다음과 같은 가져 오기 선을 놀이터에 추가하십시오.

import Foundation 
+0

감사합니다. 왜 내가 코코아를 가져올 수 없는지 아십니까? – webmagnets

+0

이 질문이 도움이 될 수 있습니다. http://stackoverflow.com/questions/24099364/no-such-module-cocoa-in-swift-playground – Vladimir

관련 문제