ios - swift indexOf for [AnyObject] array -
trying index of array ([anyobject]
). what's part i'm missing?
extension pageviewcontroller : uipageviewcontrollerdelegate { func pageviewcontroller(pageviewcontroller: uipageviewcontroller, willtransitiontoviewcontrollers pendingviewcontrollers: [anyobject]) { let controller: anyobject? = pendingviewcontrollers.first anyobject? self.nextindex = self.viewcontrollers.indexof(controller) int? } }
i have tried swift 1.2 approach:
func indexof<u: equatable>(object: u) -> int? { (idx, objecttocompare) in enumerate(self) { if let = objecttocompare as? u { if object == { return idx } } } return nil }
we need cast object we're testing uiviewcontroller
, since know array of controllers
holding uiviewcontroller
s (and know uiviewcontroller
s conform equatable
.
extension pageviewcontroller : uipageviewcontrollerdelegate { func pageviewcontroller(pageviewcontroller: uipageviewcontroller, willtransitiontoviewcontrollers pendingviewcontrollers: [anyobject]) { if let controller = pendingviewcontrollers.first as? uiviewcontroller { self.nextindex = self.viewcontrollers.indexof(controller) } } }
the logic behind error in order indexof
method compare object pass in, must compare them using ==
operator. equatable
protocol specifies class has implemented function, indexof
requires arguments conform to.
objective-c doesn't have same requirement, actual objective-c implementation ends meaning argument compared objects in array using isequal:
method (which nsobject
, therefore objective-c classes implement).
Comments
Post a Comment