gpt4 book ai didi

arrays - 如何在 swift 中用 Ms SQL 查询结果填充数组?

转载 作者:行者123 更新时间:2023-11-30 10:43:07 25 4
gpt4 key购买 nike

我是使用 Xcode 编写程序的新手。我成功地进行了 MS SQL 连接,但如何使用定义的查询结果填充数组变量。我想在 TableView 上显示我填充到 Array 变量的字符串值。我已经找到了我的代码:

import UIKit
class SecondViewController: UIViewController,SQLClientDelegate {
func error(_ error: String!, code: Int32, severity: Int32) {
print("\(error!) \(code) \(severity)")
}
@IBOutlet weak var userListTableView: UITableView!
var client:SQLClient!
var uNames: [String] = []
override func viewDidLoad() {
super.viewDidLoad()
client = SQLClient.sharedInstance()!
//Initially we call the DB data in internal memory.
strIP = UserDefaults.standard.string(forKey: "DBServerIP")!
strDBName = UserDefaults.standard.string(forKey: "DBName")!
strUName = UserDefaults.standard.string(forKey: "DBUser")!
strPass = UserDefaults.standard.string(forKey: "DBPass")!
}
@IBAction func btnUserAct(_ sender: Any) {
client = SQLClient.sharedInstance()!
client.delegate = self
client.connect(strIP,username: strUName,password: strPass,database: strDBName){ success in
if success {
self.client.execute("Select UserId from Usertbl order by Id") {
results in
for table in results as! [[[String:AnyObject]]] {
for row in table {
for (columnName, value) in row {
print(value)
//Here, UserId data appears successfully in the Xcode Debug area. So connection successful.
//How can I populate these values ​​to the uNames Array variable?
}}}
self.client.disconnect()
}}}}}
extension SecondViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return uNames.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = uNames[indexPath.row]
return cell
}}

我需要你的帮助。

最佳答案

这很简单,如果您的问题是如何向数组添加值,您可以在 Swift 中使用数组的 append 方法。 value 的类型为 AnyObject,因此您必须转换为 String。尝试这样做:

for (columnName, value) in row {
print(value)

// First option
let newVal = value as? String ?? ""
self.uNames.append(newVal)

// Second option
// self.uNames.append(value as! String)


//Here, UserId data appears successfully in the Xcode Debug area. So connection successful.
//How can I populate these values ​​to the uNames Array variable?
}

关于arrays - 如何在 swift 中用 Ms SQL 查询结果填充数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56377505/

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