How to find index of list item in Swift?

后端 未结 22 1944
离开以前
离开以前 2020-11-22 06:17

I am trying to find an item index by searching a list. Does anybody know how to do that?

I see there is list.StartIndex and <

22条回答
  •  忘掉有多难
    2020-11-22 06:32

    Swift 4

    For reference types:

    extension Array where Array.Element: AnyObject {
    
        func index(ofElement element: Element) -> Int? {
            for (currentIndex, currentElement) in self.enumerated() {
                if currentElement === element {
                    return currentIndex
                }
            }
            return nil
        }
    }
    

提交回复
热议问题