gpt4 book ai didi

swift - 我如何初始化我的数组以在任何地方进行作用域

转载 作者:搜寻专家 更新时间:2023-11-01 05:52:29 27 4
gpt4 key购买 nike

我对变量“tb”的声明有点困惑,我希望它在任何地方都有作用域。此变量来自 JSON。

如果我直接从 JSON 初始化

var tb = json as! Array<Dictionary<String, Any>>

它在工作,但它没有限定 tableView 函数的范围。

我仍然是一个快速的初学者,所以如果有人能解释我应该怎么做?以及这个范围和变量 init/type 是如何工作的,我将不胜感激。

代码如下:

class BiensViewController: UIViewController {

@IBOutlet weak var maTable: UITableView!

override func viewDidLoad() {
super.viewDidLoad()
var user = ""
var tb:[Dictionary<String, Any>]
super.viewDidLoad()
//on récupère l'utilisateur courant.
let fm = FileManager.default
let urlRepDocuments = try! fm.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let url = urlRepDocuments.appendingPathComponent("user.plist")
print(url)
if let dico = NSMutableDictionary.init(contentsOfFile: url.path){
if dico["email"] != nil {
user = dico["email"] as! String
}
}
let parameters = ["email": user]
//
guard let httpUrl = URL(string: "https://mysite.000webhostapp.com/services")else{return}
var request = URLRequest(url: httpUrl)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
guard let httpBody = try? JSONSerialization.data(withJSONObject: parameters, options: []) else { return }
request.httpBody = httpBody

let session = URLSession.shared
session.dataTask(with: request) { (data, response, error) in
if let response = response {
print("******")
print(response)
}

let json:Any
if let data = data {
do {
json = try JSONSerialization.jsonObject(with: data, options: [])
//print(json)
tb = json as! Array<Dictionary<String, Any>>
print(tb)


} catch {
print(error)
}
}

}.resume()


func numberOfSections(in tableView: UITableView) -> Int
{
return 1
}


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return tb.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
//Récupéré une cellule réutilisable
let cellule:BiensTableViewCell = tableView.dequeueReusableCell(withIdentifier: "maCellulePersonnalise", for: indexPath)
as! BiensTableViewCell

//peuple la cellule
cellule.etqRef.text = tb[indexPath.row]["ref"] as? String
cellule.EtqAdresse.text = tb[indexPath.row]["adresse"] as? String
cellule.etqCP.text = tb[indexPath.row]["cp"] as? String
cellule.etqVille.text = tb[indexPath.row]["ville"] as? String
cellule.etqNomLoc.text = tb[indexPath.row]["nom"] as? String
cellule.etqLoyer.text = tb[indexPath.row]["loyer"] as? String
return cellule

}}}

错误是:

变量 'tb' 在初始化之前被闭包捕获

最佳答案

您只需在 viewDidLoad() 外部声明变量,就在绘制导出的位置下方。目前,您的变量仅限于 viewDidLoad() 的范围。但现在它可以在整个类里面使用。

class BiensViewController: UIViewController {

@IBOutlet weak var maTable: UITableView!
var tb = [Dictionary<String, AnyObject>]()
//or you can use:- var tb = [Dictionary<String, Any>]() according to your requirement

关于swift - 我如何初始化我的数组以在任何地方进行作用域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44606455/

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