“Swift enum” Kod odpowiedzi

Swift enum

enum WeatherType {
    case sun
    case cloud
    case rain
    case wind
    case snow
}

func getHaterStatus(weather: WeatherType) -> String? {
    if weather == .sun {
        return nil
    } else {
        return "Hate"
    }
}

getHaterStatus(weather: .cloud)
Fancy Flatworm

Zdefiniuj enum Swift

Struct : Value Type | No Inheritance | No Deinitializers | No Type casting
Class : Reference Type | Inheritance supported | Has Deinitializers | Type casting

Common factors between struct and class:
Both allows to define properties to store values
Both allows to define methods
Both allows to define subscripts 
Both allows to define initializers
Both allows extension and protocols 
Vel

Czy możesz przekazać enum jako parametr do funkcji 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"
Ashamed Addax

Swift enum Nib

// There is no way to do this. But there is a little workaround.

// Swift 5:
public enum Size: String {
  case small = "small"
  case medium = "medium"
  case large = "large"
}

class SizeView: UIView {
  private var size: Size = .large

  @IBInspectable 
  public var sizeName: String = "large" {
    didSet {
      guard self.sizeName != oldValue else { return }
      self.size = Size(rawValue: self.sizeName)
    }
  }
}
Rens

Swift enum

enum Season {
  case spring, summer, autumn, winter
}
SAMER SAEID

Odpowiedzi podobne do “Swift enum”

Pytania podobne do “Swift enum”

Więcej pokrewnych odpowiedzi na “Swift enum” w Swift

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu