gpt4 book ai didi

ios - 数据被检索为project.model而不是swift ios中的元素

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

我正在尝试从 SQLite 数据库中的 DataTable 检索数据元素,但是当我检索数据时,我将数据获取为 myproject.mymodelclass,其中 WidgetData 是我的模型类,myproject 是我正在处理的项目的名称,是控制台的快照

[MyProject.WidgetData、MyProject.WidgetData、MyProject.WidgetData、MyProject.WidgetData]

但我希望 DataTable 的所有元素都应该在 ViewController 类中打印。

WidgetData 类

import Foundation

class WidgetData {

var widgetid: Int64?
var widgetname: String = ""
var widgetdescription : String = ""
var widgetform: String = ""

var formid : Int64?
var formname : String = ""
var formdescription : String = ""
var formcategory : String = ""

init(widgetid: Int64) {
self.widgetid = widgetid
}

init(formid: Int64) {
self.formid = formid
}

init(widgetid: Int64, widgetname: String, widgetdescription: String, widgetform: String) {
self.widgetid = widgetid
self.widgetname = widgetname
self.widgetdescription = widgetdescription
self.widgetform = widgetform
}

init(formid: Int64, formname : String, formdescription : String, formcategory : String) {
self.formid = formid
self.formname = formname
self.formdescription = formdescription
self.formcategory = formcategory
}
}

StephencelisDB 类

import Foundation
class WidgetData {

var widgetid: Int64?
var widgetname: String = ""
var widgetdescription : String = ""
var widgetform: String = ""

var formid : Int64?
var formname : String = ""
var formdescription : String = ""
var formcategory : String = ""

init(widgetid: Int64) {
self.widgetid = widgetid
}

init(formid: Int64) {
self.formid = formid
}

init(widgetid: Int64, widgetname: String, widgetdescription: String, widgetform: String) {
self.widgetid = widgetid
self.widgetname = widgetname
self.widgetdescription = widgetdescription
self.widgetform = widgetform
}

init(formid: Int64, formname : String, formdescription : String, formcategory : String) {
self.formid = formid
self.formname = formname
self.formdescription = formdescription
self.formcategory = formcategory
}
}

ViewController,我在其中检索值

    //SQLite start
var dataList = [WidgetData]()
var db: OpaquePointer?
var stmt: OpaquePointer?

var widgetnameV = String()
var widgetdescriptionV = String()
var widgetformV = String()

private var contacts = [WidgetData]()
private var selectedContact: Int?


func formDataSetup() {

let queryString = "INSERT INTO WidgetTable (widgetname, widgetdescription, widgetform) VALUES (?,?,?)"
//preparing the query
if sqlite3_prepare(db, queryString, -1, &stmt, nil) != SQLITE_OK{
let errmsg = String(cString: sqlite3_errmsg(db)!)
print("error preparing insert: \(errmsg)")
return
}

//binding the parameters
if sqlite3_bind_text(stmt, 1, widgetnameV, -1, nil) != SQLITE_OK{
let errmsg = String(cString: sqlite3_errmsg(db)!)
print("failure binding name: \(errmsg)")
return
}

if sqlite3_bind_text(stmt, 2, widgetdescriptionV, -1, nil) != SQLITE_OK {
let errmsg = String(cString: sqlite3_errmsg(db)!)
print("failure binding name: \(errmsg)")
return
}

if sqlite3_bind_text(stmt, 3, widgetformV, -1, nil) != SQLITE_OK {
let errmsg = String(cString: sqlite3_errmsg(db)!)
print("failure binding name: \(errmsg)")
return
}

//executing the query to insert values
if sqlite3_step(stmt) != SQLITE_DONE {
let errmsg = String(cString: sqlite3_errmsg(db)!)
print("failure inserting hero: \(errmsg)")
return
}

}



func readValues(){

//first empty the list of heroes
// heroList.removeAll()

//this is our select query
let queryString = "SELECT * FROM WidgetTable"

//

print(dataList)

//statement pointer
var stmt:OpaquePointer?

//preparing the query
if sqlite3_prepare(db, queryString, -1, &stmt, nil) != SQLITE_OK{
let errmsg = String(cString: sqlite3_errmsg(db)!)
print("error preparing insert: \(errmsg)")
return
}

//traversing through all the records
while(sqlite3_step(stmt) == SQLITE_ROW){
let widgetid = sqlite3_column_int(stmt, 0)
let widgetnameC = String(cString: sqlite3_column_text(stmt, 1))
let widgetdescriptionC = String(cString: sqlite3_column_text(stmt, 2))
let widgetformC = String(cString: sqlite3_column_text(stmt, 3))

//adding values to list
dataList.append(WidgetData(widgetid: Int64(Int(widgetid)), widgetname: String(describing: widgetnameC), widgetdescription: String(describing: widgetdescriptionC), widgetform: String(describing: widgetformC)))
print(dataList)
}

}

最佳答案

实现 description var 来打印类属性

class WidgetData:NSObject
{
var tripId: String
var name: String

init(tripId: String ,name:String)
{
self.tripId = tripId
self.name = name

}
override var description: String
{

return "\(self.tripId) \(self.name)"
}

}

更新

你的问题是你有多个 init ,首先我建议将数据隔离到其他类,或者在描述中检查每个 init 中的第一项并使用!如果所有的初始值都不为零

关于ios - 数据被检索为project.model而不是swift ios中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48745527/

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