Skip to content

Latest commit

 

History

History
33 lines (26 loc) · 568 Bytes

Conditionally-Extending-a-Type.md

File metadata and controls

33 lines (26 loc) · 568 Bytes

Conditionally Extending a Type

Conform to the OptionSetType protocol

extension SequenceType where
  Generator.Element : IntegerArithmeticType{
  public func canFind(value: Generator.Element) -> Bool{
    for (_, v) in self.enumerate(){
      if v == value{
        return true
      }
    }
    return false
  }
}

You can use it like so:

func example1(){

    if [1, 3, 5, 7].canFind(5){
      print("Found it")
    } else {
      print("Could not find it")
    }

  }

The above examples are from "iOS 9 Swift Programming Cookbook".