gpt4 book ai didi

ios - 在另一种方法 Swift 中使用 Post 请求响应字符串

转载 作者:行者123 更新时间:2023-11-28 06:56:49 25 4
gpt4 key购买 nike

我正在尝试在 prepareForSegue 函数中使用发布请求的响应。我想将响应值分配给目标 viewController 属性。我不能那样做,因为当我在侧闭括号中分配它时,在查看新窗口之前不会分配。因为在关闭代码中读取,在查看新窗口之后。

以及我尝试将 saveInvoice 函数分配给一个变量并在 prepareForSegue 函数中使用。但该变量是 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)
}

这是处理post请求的函数

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
}

最佳答案

首先,saveInvoice 是异步执行的,因此在下一个 Controller 加载时您不会获得值,但会在稍后加载。

要保存变量,您需要在完成 block 中执行此操作。我猜这里感兴趣的是 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
}

关于ios - 在另一种方法 Swift 中使用 Post 请求响应字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33360826/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com