0

I am trying to retrieve the data elements from DataTable in SQLite database but when I retrieve the data I am getting the data as myproject.mymodelclass where WidgetData is my model class and myproject is the the name of my project on which I am working here is the snapshot of console

[MyProject.WidgetData, MyProject.WidgetData, MyProject.WidgetData, MyProject.WidgetData]

But I want all the elements of DataTable should be printed in ViewController class.

WidgetData Class

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 class

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 in which I am retrieving the values

    //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)
    }

}
iOS Developer
  • 311
  • 4
  • 25
  • 1
    What is the question? – vadian Feb 12 '18 at 11:38
  • Possible duplicate of [How can I change the textual representation displayed for a type in Swift?](https://stackoverflow.com/questions/24068506/how-can-i-change-the-textual-representation-displayed-for-a-type-in-swift). – Martin R Feb 12 '18 at 12:05
  • @MartinR its obvious that the mentioned answer is about structs and I am not dealing with structs. – iOS Developer Feb 12 '18 at 12:57
  • @iOSDeveloper: Adopting the CustomStringConvertible protocol by implementing the description method works for both classes and structs, there is no difference at all. – Did you try it? – Martin R Feb 12 '18 at 12:59
  • @vadian updated the question and given answer kindly view here – iOS Developer Feb 13 '18 at 07:23

1 Answers1

0

implement description var to print the class properties

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)"
    }

}

Update

Your problem is that you have more than one init , first I advice isolating the data to other class , or in description check first item in every init and use ! if all the init values are not nil

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87