How to remove duplicate object after appending new one in swift -


im new in swift , i've gathered lot of data here build small project. @ place i'm bit lost , ask this:

i save color , depending mixing ratio object (which plan save in phone memory, i'm not far now). can without problems. when add same color other mixing ratio delete previous created (same) color.

that's point. dont know how delete previous one. have till (greatful forum) this:

init stuff:

class main{      var colorcollection:[color]!      init() {         colorcollection = [color]()     }      func addcolor(icolorname:color) {         colorcollection.append(icolorname)     }  }  class color{      var colorname:string!      var ingredient:[ingredients]!      init() {         ingredient = [ingredients]()     }      func addingredient(iingredient:string) {          var tmpingredient = ingredients()         tmpingredient.ingredient = iingredient          ingredient.append(tmpingredient)      }      func arraycount(){         println("ingredients   : \(ingredient.count)")     } }  class ingredients{     var ingredient:string! }   var cl = main() var ingredient = color() 

so here i'm creating objects so:

cl.addcolor(ingredient)  ingredient.colorname = "red" ingredient.addingredient("20 ml: magenta") ingredient.addingredient("21 ml: yellow")  ingredient = color() 

after few wile user decide change color in gui , create new object:

cl.addcolor(ingredient)  ingredient.colorname = "red" ingredient.addingredient("20 ml: magenta") ingredient.addingredient("22 ml: yellow") ingredient.addingredient("51 ml: black")  ingredient = color() 

printing it:

for color in bn.colorcollection {     println("color: \(color.colorname)")      ingr in color.ingredient {         println(" -> ingredient   : \(ingr.ingredient)")     }     color.arraycount() }  

and result is:

color: red  -> ingredient   : 20 ml: magenta  -> ingredient   : 21 ml: yellow ingredients   : 2 color: red  -> ingredient   : 20 ml: magenta  -> ingredient   : 22 ml: yellow  -> ingredient   : 51 ml: black ingredients   : 3 

so see, there 2 red's have newly created one. amazing. thx

you have modify addcolor function this:

func addcolor(icolorname:color) {     var pos = -1      // find if color same name in array     (index, color) in enumerate(colorcollection) {         if (color.colorname == icolorname.colorname) {             pos = index             break         }     }      if (pos > -1) {         // if found, replace new color object         colorcollection[pos] = icolorname     } else {         // otherwise, add         colorcollection.append(icolorname)     } } 

but won't need right away. because code really bad.


what's wrong code:

  1. crazy naming convention: colorname string icolorname object of type color; ingredients class ingredient array; later, define variable ingredient type color iingredient string! wanna throw book @ now!

2. color has not been set before being sent object.

var ingredient = color() cl.addcolor(ingredient)         // no name , no ingredients ingredient.colorname = "red" ingredient.addingredient("20 ml: magenta") ingredient.addingredient("21 ml: yellow") 

the object may clone color before adding array, in case subsequent statements have no effect upon it. should finish setting object before passing object:

var ingredient = color() ingredient.colorname = "red" ingredient.addingredient("20 ml: magenta") ingredient.addingredient("21 ml: yellow") cl.addcolor(ingredient)         // good! 
  1. color no name: colorname property defined optional, let did. expected color may have no name?

Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Website Login Issue developed in magento -

Can the constants be defined inside a model file of a framework in PHP? -