/创建NSURL对象 let urlString:String="http://hangge.com" let url:NSURL! = NSURL(string:urlString) //创建请求对象 let request:NSURLRequest = NSURLRequest(URL: url) let session = NSURLSession.sharedSession() // 设置信号开始 let semaphore = dispatch_semaphore_create(0) let dataTask = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) -> Void in if error != nil{ print(error?.code) print(error?.description) }else{ let str = NSString(data: data!, encoding: NSUTF8StringEncoding) print(str) } //设置信号结束 dispatch_semaphore_signal(semaphore) }) as NSURLSessionTask //使用resume方法启动任务 dataTask.resume() //等待完成.. dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER) print("数据加载完毕!") //继续执行其他代码.......