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   } 

type 'anyobject?' not conform protocol 'equatable' cannot assign immutable value of type 'int?'

we need cast object we're testing uiviewcontroller, since know array of controllers holding uiviewcontrollers (and know uiviewcontrollers 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

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

python - build a suggestions list using fuzzywuzzy -