pass closure as struct parameter in swift code example
Example 1: closure swift
// The basic closure syntax is:
// { (parameter: Type) -> Type in <statements> }
//
// While closures can be assigned to a variable like in
// the example below, they become much more useful once
// they're passed into a function such as a list's `filter`
// method.
let closure = { (a: Int, b: Int) -> Int in return a * b }
print(closure(3, 6)) // 18
print(closure(10, 2)) // 20
Example 2: can you pass an enum as a parameter to a function swift
func justAnExample<T : RawRepresentable>(_ enumType: T.Type) where T.RawValue == Int {
// Note that an explicit use of init is required when creating an instance from a
// metatype. We're also using a guard, as `init?(rawValue:)` is failable.
guard let val = enumType.init(rawValue: 0) else { return }
print("description: \(val)")
}
justAnExample(Foo.self) // prints: "description: Hello Foo"
justAnExample(Bar.self) // prints: "description: Hello Bar"