我需要我的responseString的值,因为它包含我将发送给下一个ViewController的数据,我已经在completionHandlers上读过,我还发现某处我的responseString数据只能存在于变量中,是否有任何变通方法因为我从没想过这会成为一个重大问题。
func GenerateData
var responseString = "baddata"
let request = NSURL(string: postString)
// NSLog("We are getting" + "this :" + postString )
//request.HTTPMethod = "POST"
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL((request)!, completionHandler: { (data: NSData?, response:NSURLResponse?,
error: NSError?) -> Void in
//do something
if let httpStatus = response as? NSHTTPURLResponse where httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(response)")
}
responseString = NSString(data: data! , encoding: NSUTF8StringEncoding) as! String
print("responseString = \(responseString)")
})
task.resume()
return responseString
}
答案 0 :(得分:-1)
像这样使用Session。
添加此控制器顶部
let prefs:NSUserDefaults = NSUserDefaults.standardUserDefaults()
查找
print("responseString = \(responseString)")
稍后添加
prefs.setObject(responseString, forKey: "responseString")
prefs.synchronize()
现在你的responseString准备好了。 打开另一个控制器视图控制器并执行此操作。
并像这样使用
let yourString:NSString = "\( prefs.valueForKey("responseString") as! String!)"
self.responseText.text = yourString as String