SWIFT append data to dictionary -
i'm having trouble coding apparently simple task. want add new client profile data client profile dictionary (clientdatabase) keep getting errors - can't seem append - error: value of type '(string: clientprofile)' has no member 'append' (see error @ bottom of code)
any insights can provide appreciated.
thanks, bill
//: playground - noun: place people can play import uikit import foundation /* code copied b-c dev database - structs simplified fewer variables goal getappend new client work. */ /* globals: these go in main.swift file */ struct clientprofile { var firstname: string = "" var lastname: string = "" var group: int = 0 } //var clientdatabase : nsmutabledictionary! = [string:clientprofile]() var clientdatabase:[string:clientprofile] /* sample data template: phone key, sub array is; (firstname: "", lastname: "",pilatesgroup: ) */ clientdatabase = [ "1234567": clientprofile(firstname: "sally", lastname: "sillious", group: 3), "2345678": clientprofile(firstname: "sue", lastname: "parker",group: 8), "3456789": clientprofile(firstname: "bob", lastname: "parker", group: 2), "5678901": clientprofile(firstname: "jim", lastname: "beam", group: 12) ] clientdatabase.count // far /* add new client declare local variables in scene swift files used */ var firstname: string = "" var phone:string = "" var newphone: string = "" var newfirstname: string = "" var newlastname: string = "" var newgroup: int = 0 // define struct using these input variables values same keys (global) clientdatabase struct newclientprofile { var firstname: string = newfirstname var lastname: string = newlastname var group: int = newgroup } // put newclientprofile newclientdictionary var newclientdatabase:dictionary = [string:newclientprofile]() // input values scene - uitextfields newphone = "4567890" newfirstname = "super" newlastname = "dave" newgroup = 14 // test values should clientdatabase clientdatabase.count newclientdatabase = [newphone:newclientprofile()] newclientdatabase.count // ok far //the following line returns error clientdatabase.append(newclientdatabase) // can't seem append - error value of type '(string: clientprofile)' has no member 'append'
two things. first of clientdatabase
dictionary doesn't have append
, instead you'll have iterate through other dictionary , insert elements clientdatabase
.
the other issue clientdatabase
, newclientdatabase
aren't same type. first 1 [string : clientprofile]
, second [string : newclientprofile]
. you'll have convert values 1 type other combine dictionaries.
looking deeper code there misunderstandings language. example:
struct newclientprofile { var firstname: string = newfirstname var lastname: string = newlastname var group: int = newgroup } // put newclientprofile newclientdictionary var newclientdatabase:dictionary = [string:newclientprofile]()
you're creating struct purpose of containing single set of values when have clientprofile
. instead do:
var newclientprofile = clientprofile(firstname: newfirstname, lastname: newlastname, group: newgroup)
this create variable instance of clientprofile
, stores information want. however, have other variables defined empty values.
here's cleaned version of code, take @ , let me know if have questions.
struct clientprofile { // convention use initial caps enum, struct, class let firstname: string let lastname: string let group: int } var clientdatabase = [ "1234567": clientprofile(firstname: "sally", lastname: "sillious", group: 3), "2345678": clientprofile(firstname: "sue", lastname: "parker",group: 8), "3456789": clientprofile(firstname: "bob", lastname: "parker", group: 2), "5678901": clientprofile(firstname: "jim", lastname: "beam", group: 12) ] // input values scene - uitextfields let newphone = "4567890" let newfirstname = "super" let newlastname = "dave" let newgroup = 14 // define struct using these input variables values same keys (global) clientdatabase let newclientprofile = clientprofile(firstname: newfirstname, lastname: newlastname, group: newgroup) let newclientdatabase = [newphone:newclientprofile] (phone,client) in newclientdatabase { clientdatabase[phone] = client }
Comments
Post a Comment