swift - Changing one element in array changes all elements in the array -
i new swift , i'm trying mess around uitableviews , arrays.
i have array of type [day]
, named daysarray
. i'm initiating array in viewdidload()
, daysarray
has 7 "days" in it.
also, have uitableview
, being populated daysarray
. when i'm trying change 1 day (one element in array), whole daysarray
days changed , consequently, cells in uitableview
same.
peculiar, don't know wrong (:
the day class:
import foundation class day { private var _name: string! private var _starts: double! private var _ends: double! var name: string! { { if _name == nil { _name = "sunday" } return _name } set { _name = newvalue } } var starts: double! { { if _starts == nil { _starts = 8.00 } return _starts } set { _starts = newvalue } } var ends: double! { { if _ends == nil { _ends = 20.00 } return _ends } set { _ends = newvalue } } init(daydict: dictionary<string, anyobject>) { if let dictname = daydict["name"] as! string! { self._name = dictname } if let dictisworking = daydict["isworking"] as! bool! { self._isworking = dictisworking } if let dictstarts = daydict["starts"] as! double! { self._starts = dictstarts } if let dictends = daydict["ends"] as! double! { self._ends = dictends } } }
the code seems problematic is:
import uikit let generaldaydict: [string : anyobject] = ["name" : "sunday" anyobject, "starts": 8.00 anyobject, "ends" : 20.00 anyobject] let someday: day = day(daydict: generaldaydict) class viewcontroller: uiviewcontroller { var daysarray: [day]! = [] override func viewdidload() { super.viewdidload() in 0...6 { let generalday: day = someday daysarray.append(generalday) } changearray() // additional setup after loading view, typically nib. } func changearray() { daysarray[5].starts = 6.00 day in daysarray { print("each day in array starts at: ", day.starts) } } }
the print command in changearray prints this:
each day in array starts at: 6.0 each day in array starts at: 6.0 each day in array starts at: 6.0 each day in array starts at: 6.0 each day in array starts at: 6.0 each day in array starts at: 6.0 each day in array starts at: 6.0
as said, very peculiar... thank reading question, , answering of course (:
in loop instantiate objects same reference. have replace :
let generalday: day = someday
by :
let generalday = day(daydict: generaldaydict)
then able change attributes of generalday individually
Comments
Post a Comment