ios - Filter array of custom objects in Swift -
i'm trying filter array of custom objects in swift subset of data has properties want isolate. code follows.
func generatesubset( datapool : [customobject]) -> [customobject]? { let subsetdata = datapool.filter{(includeelement:customobject)-> bool in return contains(includeelement.position, "teacher") } return subsetdata }
my custom object follows:
class customobject : { var position : string? init(){ position = "" } }
however error xcode throws me when trying compile code is:
cannot invoke 'filter' argument list of type [customobject] -> bool
i'm using swift 1.2 , can't seem figure out i'm doing wrong. appreciated.
in swift 1.2, filter
global function, can't datapool.filter(...)
. (in swift 2, work.)
furthermore, contains
can't used strings that. recommend using rangeofstring:
method nsstring:
let teachers = filter(datapool) { // in swift 2 "datapool.filter {" return $0.position!.rangeofstring("teacher") != nil }
Comments
Post a Comment