ios - Extending Lists in Swift -
i'm developing backend part of system using apple's swift needs deal email lists (an array of email objects) , need set own properties list such allowrepeated: bool
, , methods performcleanup()
remove duplicate entries if allowrepeated
property set true
.
i have done in playground trying follow documentation notes extending nsmutablearray class:
class emaillist: nsmutablearray { override var count: int { { // seems line below generates infinity loop return nsarray(array: self).count } } override func insertobject(anobject: anyobject, atindex index: int) { insertobject(anobject, atindex: index) } override func removeobjectatindex(index: int) { super.removeobjectatindex(index) } override func addobject(anobject: anyobject) { super.addobject(anobject) } override func removelastobject() { super.removelastobject() } override func replaceobjectatindex(index: int, withobject anobject: anyobject) { super.replaceobjectatindex(index, withobject: anobject) } override func objectatindex(index: int) -> anyobject { return super.objectatindex(index) } }
it seems line return nsarray(array: self).count
causing infinite loop. why happening?
cocoa collection classes (such nsmutablearray) what's known "class clusters" , not meant subclassed. guarantee malfunctions due subclassing nsmutablearray.
in case, i'm guessing when strange count
override tries create array own contents, it's once again asked count
when array(array:)
constructor called, since array(array:)
presumably needs know how long it'll based on passed source array. lather, rinse, repeat.
the real question why short-circuit basic cocoa class in way? possible gains feel you're getting causing creation of whole new array when nsmutablearray.count
works fine? don't things this.
Comments
Post a Comment