我正在尝试在prepareForSegue
函数中使用post请求的响应。我想将响应值分配给目标viewController属性。我不能这样做,因为当我在侧边括号中分配它时,在查看新窗口之前不会分配。因为在封闭代码内部读取后,查看新窗口。
以及我尝试将saveInvoice函数赋值给变量并在NSUrlSessionDataTask
函数中使用。但该变量为override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let previewVC :PreviewViewController = segue.destinationViewController as! PreviewViewController
previewVC.invoiceSummary = "test hash key"
let invoiceSave = saveInvoice({ hash_key in
print(hash_key)
let test = hash_key
if test != ""
{
print("Success")
}
previewVC.sendersName = "sender view controller name"
})
print(invoiceSave)
}
类型,因此我无法进一步使用它。
下面我提到了我的代码段
调用函数
func saveInvoice(completionHandler: (NSString) -> Void) -> NSURLSessionDataTask
{
let invoiceSummary = "Sample invoice summary"
let invoiceDate = "2015-11-20"
let invoiceConnectionID = "647193"
//let json = ["summary": invoiceSummary, "date": invoiceDate, "business_id": invoiceConnectionID]
let json = NSMutableDictionary()
json.setValue(invoiceSummary, forKey: "summary")
json.setValue(invoiceDate, forKey: "2015-11-20")
json.setValue(invoiceConnectionID, forKey: "business_id")
let data = try? NSJSONSerialization.dataWithJSONObject(json, options: NSJSONWritingOptions(rawValue: 0));
let request = NSMutableURLRequest(URL: NSURL(string: "https://livetest.somedomain.com/api/invs?auth_token=jmkm6ijijejf23kmkdd")!)
request.HTTPMethod = "POST"
request.addValue("application/json",forHTTPHeaderField: "Content-Type")
request.addValue("application/json",forHTTPHeaderField: "Accept")
request.HTTPBody = data
var outer = ""
let task = NSURLSession.sharedSession().dataTaskWithRequest(request){
data, response, error in
let responceJSon = JSON(data: data!)
//let text = self.passJson(responceJSon)
let invoice = responceJSon["invoice"]
let hash_key = invoice["hash_key"].stringValue
let statement_number = invoice["statement_no"].stringValue
let statement_summary = invoice["summary"].stringValue
let statement_date = invoice["date"].stringValue
let obj = ["hash_key": hash_key, "statement_no": statement_number, "summary": statement_summary, "date": statement_date]
self.invObject.append(obj)
//self.invObject.append(text as! [String : String])
outer = self.invObject[0]["statement_no"]!
print(outer)
if let hash_key = invoice["hash_key"].string{
completionHandler(hash_key)
return
}
}
task.resume()
return task
}
这是处理发布请求的功能
{{1}}
答案 0 :(得分:1)
首先,saveInvoice是异步执行的,所以当下一个控制器加载但不久之后你就不会获得该值。
要保存变量,您需要在完成块内执行此操作。我想这是感兴趣的hash_key
吗?
所以你会做这样的事情
saveInvoice({ hash_key in
previewVC.hash_key = hash_key
previewVC.sendersName = "sender view controller name"
})
但是,如上所述,这是异步执行所以我宁愿认为最好开始保存,然后在从saveInvoice中调用完成处理程序时以编程方式执行segue
saveInvoice({ hash_key in
savedHashKey = hash_key
self.performSegueWithIdentifier(segueName, sender: self)
})
然后在prepareForSegue
中let savedHashKey:String? = nil
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let previewVC :PreviewViewController = segue.destinationViewController as! PreviewViewController
previewVC.invoiceSummary = savedHashKey
savedHashKey = nil
}