gpt4 book ai didi

ios - Swift PHP post请求从任务中设置全局变量

转载 作者:行者123 更新时间:2023-11-29 00:51:33 25 4
gpt4 key购买 nike

我使用 post 请求成功地从数据库中检索数据。 (我不想使用获取请求,因为我想向 php 发送验证。)不要担心 php 部分,它应该没问题。

import UIKit

class mainPage: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var toolBar: UIToolbar!
var values:NSArray = []

@IBOutlet weak var Open: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()


Open.target = self.revealViewController()
Open.action = #selector(SWRevealViewController.revealToggle(_:))
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
get()

}
func get(){
let request = NSMutableURLRequest(URL: NSURL(string: "http://www.percyteng.com/orbit/getAllpostsTest.php")!)
request.HTTPMethod = "POST"
let postString = "user=\("ios")"
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in
if error != nil {

print("error=\(error)")
return
}

print("response = \(response)")

let array = try! NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! NSArray
self.values = array
}
task.resume()
dispatch_async(dispatch_get_main_queue()) { tableView.reloadData()}
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if values.count > 20{
return 20
}
else{
return values.count
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as!postCell

let maindata = values[values.count-1-indexPath.row]
if maindata["category"] as? String == "Services"{
cell.postImg.image = UIImage(named: "tile_services")
}
else if maindata["category"] as? String == "exchange"{
cell.postImg.image = UIImage(named: "tile_exchange")
}
else if maindata["category"] as? String == "Tutors"{
cell.postImg.image = UIImage(named: "tile_tutoring")
}
else if maindata["category"] as? String == "Sports"{
cell.postImg.image = UIImage(named: "tile_sports")
}
else if maindata["category"] as? String == "Sublet"{
cell.postImg.image = UIImage(named: "tile_sublet")
}
else if maindata["category"] as? String == "Events"{
cell.postImg.image = UIImage(named: "tile_events")
}
else{
cell.postImg.image = UIImage(named: "tile_carpool")
}


if maindata["category"] as? String == "Services" || maindata["category"] as? String == "Tutors" || maindata["category"] as? String == "Events"{
cell.title.text = maindata["title"] as? String
}
else if maindata["category"] as? String == "Sublet" || maindata["category"] as? String == "Rideshare"{
cell.title.text = maindata["location"] as? String
}
else{
cell.title.text = maindata["item"] as? String
}


if maindata["category"] as? String == "Sublet" || maindata["category"] as? String == "Rideshare"{
cell.location.text = ""
}
else{
cell.location.text = maindata["location"] as? String
}
cell.category.text = maindata["category"] as? String
cell.price.text = maindata["price"] as? String
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
}
}

所以我有一个名为 values 的全局变量,它是一个 NSArray,我想将这个数组的值设置为我从数据库中检索的数组。但是,在函数 get() 中,post 请求充当另一个线程,我必须编写 self.values = array,它不会更改我的全局变量的值。

我需要该值来在主数组中组织我的 TableView 。

基本上,我的问题是如何从闭包中获取值并将其设置为全局变量。

谢谢!如果你们不明白我在说什么,请告诉我。

最佳答案

当您执行 dataTaskWithRequest 时,您需要在 func get() 中有一个完成处理程序,这是一个 Async 调用。试试这个:

func get(finished: (isDone: Bool) -> ()){
//your code
data, response, error in
if error != nil {
finished(isDone: false)
print("error=\(error)")
return
}


print("response = \(response)")

let array = try! NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! NSArray
self.values = array
}
}
task.resume()
finished(isDone: true)
}

然后在您的 viewDidLoad 中:

 override func viewDidLoad() {
super.viewDidLoad()
get { success in
if success{
//reload your tableview here
}

}

关于ios - Swift PHP post请求从任务中设置全局变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38089247/

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