xcode Swift-使用未解析的标识符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37802357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift- Use of unresolved identifier
提问by zam
I have the following Swift code where I am sending a POST request to a webserver and receiving a value back. The error that I receive is on the second to last line saying "Use of unresolved identifier"
我有以下 Swift 代码,我在其中向网络服务器发送 POST 请求并接收返回值。我收到的错误在倒数第二行说“使用未解析的标识符”
func download_request() -> String {
let url:NSURL = NSURL(string: "http://url.com/read.php")!
let session = NSURLSession.sharedSession()
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
request.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringCacheData
let paramString = "data=name"
request.HTTPBody = paramString.dataUsingEncoding(NSUTF8StringEncoding)
let task = session.downloadTaskWithRequest(request) {
(let location, let response, let error) in
guard let _:NSURL = location, let _:NSURLResponse = response where error == nil else {
print("Error")
return
}
let urlContents = try! NSString(contentsOfURL: location!, encoding: NSUTF8StringEncoding)
guard let _:NSString = urlContents else {
print("Error")
return
}
}
task.resume()
return urlContents
}
How would I fix this error? I think that it means that I cannot use urlContents outside of the let task = session.downloadTaskWithRequest(request) { } but how would I declare the variable so that I can use it outside?
我将如何修复此错误?我认为这意味着我不能在 let task = session.downloadTaskWithRequest(request) { } 之外使用 urlContents 但是我如何声明变量以便我可以在外面使用它?
回答by Ike10
Try declaring urlContents
outside of the session.downloadTaskWithRequest(request) { }
block. Like so:
尝试urlContents
在session.downloadTaskWithRequest(request) { }
块之外声明。像这样:
func download_request() -> String? {
let url:NSURL = NSURL(string: "http://url.com/read.php")!
let session = NSURLSession.sharedSession()
var urlContents: NSString?
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
request.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringCacheData
let paramString = "data=name"
request.HTTPBody = paramString.dataUsingEncoding(NSUTF8StringEncoding)
let task = session.downloadTaskWithRequest(request) {
(let location, let response, let error) in
guard let _:NSURL = location, let _:NSURLResponse = response where error == nil else {
print("Error")
return
}
urlContents = try? NSString(contentsOfURL: location!, encoding: NSUTF8StringEncoding)
guard let _:NSString = urlContents else {
print("Error")
return
}
}
task.resume()
return (urlContents as! String)
}
Then when you need to use urlContents
unwrap it with an if let binding
然后当你需要使用urlContents
if let 绑定解包时
if let contents = download_request() {
//Use safely unwrapped contents
}
回答by Santosh
You could make return type optional
like below
你可以optional
像下面这样设置返回类型
func download_request() -> String? {
var urlContents: String?
....
....
let task = session.downloadTaskWithRequest(request) {
(let location, let response, let error) in
...
...
...
guard let _:NSURL = location, let _:NSURLResponse = response where error == nil else {
print("Error")
return
}
urlContents = try! NSString(contentsOfURL: location!, encoding: NSUTF8StringEncoding) as String
guard let _:NSString = urlContents else {
print("Error")
return
}
}
task.resume()
return urlContents
}
And call the method like
并调用方法,如
if let request = download_request() {
//do anything with your request
}
Suggesting you to rename your method properly since you are returning the downloaded data not the request.
建议您正确重命名您的方法,因为您返回的是下载的数据而不是请求。
And this is not the correct way to get your downloaded data back as a return to the method since the completionHandler
will be called once the operation is done so urlContents
will be nil. Hence create another method which takes String
as input and call it within the completionHandler
of downloadTaskWithRequest
这不是将下载的数据取回作为方法返回的正确方法,因为completionHandler
一旦操作完成urlContents
就会调用它,因此它将为零。因此创建另一个方法,它String
作为输入并在completionHandler
ofdownloadTaskWithRequest