Search
 
SCRIPT & CODE EXAMPLE
 

SWIFT

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"
Comment

PREVIOUS NEXT
Code Example
Swift :: swiftui menu 
Swift :: swiftui hidden 
Swift :: float vs double in swift 
Swift :: adding label to navigation bar 
Swift :: swift uilabel font bold 
Swift :: swft view 
Swift :: swift extension 
Swift :: Swift How to declare an optional in Swift? 
Swift :: swift pass any closer to the function 
Swift :: swift array in chunks 
Swift :: Swift Overloading with Different Parameter Types 
Swift :: Swift Access Struct Properties Swift 
Swift :: swift print struct name 
Swift :: uitableview total number of rows 
Swift :: Swift Equatable Protocol 
Swift :: Swift Function overloading with Argument Label 
Swift :: Swift self property 
Swift :: Declare Constants in Swift 
Swift :: swift uknow attrubute main 
Swift :: Assign values to enum variables Swift 
Swift :: Javascript Define Swift Class 
Swift :: octobercms add extra slash to css url 
Swift :: load plist swift 
Ruby :: kill port already in use 
Ruby :: iterate over string ruby 
Ruby :: rails generate model polymorphic references 
Ruby :: ruby iterate over array 
Ruby :: rails check_box_tag 
Ruby :: rails order 
Ruby :: rails resources only 
ADD CONTENT
Topic
Content
Source link
Name
4+5 =