ios - set NSArray instance using reference in Swift -
i'm trying set values nsarray using reference in swift can't seem working.
var list = self.isfiltered ? self.filteredeventslist : self.eventslist self.sorttableviewforeventname(&list)
then:
private func sorttableviewforeventname(inout eventarray: nsarray) { let sortedevents = eventarray.sortedarrayusingcomparator({ (firstevent, secondevent) in let firsteventname = (firstevent as! event).name let secondeventname = (secondevent as! event).name return firsteventname.compare(secondeventname) }) eventarray = sortedevents }
the change in eventarray not reflect in eventslist or filteredeventslist, whichever may applicable.
you need have reason using inout
parameters, , in case don't think have one. can have function return sorted array:
private func sorttableviewforeventname(eventarray: nsarray) -> nsarray { let sortedevents = eventarray.sortedarrayusingcomparator({ (firstevent, secondevent) in let firsteventname = (firstevent as! event).name let secondeventname = (secondevent as! event).name return firsteventname.compare(secondeventname) }) return sortedevents }
then say
var list = self.isfiltered ? self.filteredeventslist : self.eventslist list = self.sorttableviewforeventname(list)
but, unless have reason using nsarray
, can use swift array , gain type awareness/safety , more concise sorting:
private func sorttableviewforeventname(eventarray: [event]) -> [event] { let sortedevents = eventarray.sort { $0.name < $1.name } return sortedevents }
Comments
Post a Comment