ios - How to store database into struct using swift3? -


i have function database , return in mutablearray, need database in struct.

do need mutablearray struct or should data straight struct?

i have no idea how approach or how store database struct

my code:

class crimesinfo: nsobject {  var name: string = string() var detail: string = string() var time: string = string() } 

the function:

func getallcrimesdata() -> nsmutablearray {     sharedinstance.database!.open()     let resultset: fmresultset! = sharedinstance.database!.executequery("select * crimetable", withargumentsin: nil)     let marrcrimesinfo : nsmutablearray = nsmutablearray()     if (resultset != nil) {         while resultset.next() {             let crimesinfo : crimesinfo = crimesinfo()             crimesinfo.name = resultset.string(forcolumn: "name")             crimesinfo.detail = resultset.string(forcolumn: "detail")             crimesinfo.time = resultset.string(forcolumn: "time")             marrcrimesinfo.add(crimesinfo)         }     } 

there's lot going on here. here of thoughts:

  1. your getallcrimesdata gets crimesinfo. "data" or "into, pick one, stay consistent.
  2. your getallcrimesdata return array of kinds of data mushed together. there's no need this, fill out structs immediately, rather worrying parsing array structs later.
  3. there's never reason use nsarray, nsdictionary, or mutable versions in swift. use native swift type.
  4. use safe guard let or if let checking deal optionality of resultset, rather using implicitly-unwrapped optional.
  5. there's no reason make crimesinfo class
  6. there's no reason make crimesinfo inherit nsobject
  7. there's no reason pre-fil name/detail/time empty string. doubt want crime named "".

here's how write this:

struct crimeinfo: nsobject {     let name: string     let details: string     let time: string      init(name: string, detail: string, time: string) {         self.name = name         self.details = details         self.time = time     }      init(fromresultset: fmresultset) {         self.init(             name: resultset.string(forcolumn: "name"),             details: resultset.string(forcolumn: "detail"),             time: resultset.string(forcolumn: "time")         )     } }  let crimesintoquery = "select * crimetable"  func getallcrimesinfo() -> [crimeinfo] {     let database = sharedinstance.database!     database.open()      guard let resultset = database.executequery(crimesintoquery, withargumentsin: nil) {         else return []     }      var crimes = [crimeinfo]()      while resultset.next() {         crimes.add(crimeinfo(fromresultset: resultset))     }      return crimes } 

Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -