initialization - Custom initialisation with NSMutableArray in Swift -
i have custom scrollview init
below in swift
class customscrollview:uiscrollview,uiscrollviewdelegate { private allow unitlabelwidth:cgfloat = kscreenwidth / 3 private allow displayedunits:nsmutablearray? init(frame: cgrect,unitdata:nsmutablearray,scrolltype:int) { super.init(frame: frame) unitdata.addobject("") unitdata.addobject("") self.pagingenabled = false self.delegate = self self.scrollidentifier = scrolltype self.displayedunits = unitdata } required init(coder adecoder: nscoder) { fatalerror("init(coder:) has not been implemented") } }
as can see, "unitdata
" nsmutablearray
parameter passed , adding objects array in custom init
. it's simple one.
now when creating objects of scrollview
class, getting output not able comprehend.
take @ below code
func unitcreation(){ allow units:nsmutablearray = nsmutablearray(objects:"unit1","unit2") allow unitlabelwidth = kscreenwidth / 3 allow fromunitscroll = customscrollview(frame: cgrectmake(0, 0, kscreenwidth, scrollheight),unitdata: units,scrolltype:1) mainview?.addsubview(fromunitscroll) println(units) allow tounitscroll = customscrollview(frame: cgrectmake(0, fromunitscroll.frame.origin.y + scrollheight * 3, kscreenwidth, scrollheight),unitdata: units,scrolltype:2) mainview?.addsubview(tounitscroll) }
i have array constant called "units
" 2 objects "unit1
" , "unit2
". when pass "units
" create object "fromunitscroll
",data passed ["unit1","unit2"]
now printing "units
". here see weird result. expecting see printed output ["unit1","unit2"]
in original assignment see ["unit1","unit2","",""]
.
objects added in custom init method reflected in println statement.
i passing "units
" parameter custom init method , not able why add-on of objects init
method reflected in "unitcreation
" method
thank you
this because nsmutablearray class swift , such passed reference. modifications in init
functions modify original array. using swift arrays, passed value, not observe this.
swift initialization
No comments:
Post a Comment