“Свифт Энум” Ответ

Свифт Энум

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

Определите 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

Можете ли вы передать перечисление в качестве параметра в функцию 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

Свифт Энум

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

Ответы похожие на “Свифт Энум”

Вопросы похожие на “Свифт Энум”

Больше похожих ответов на “Свифт Энум” по Swift

Смотреть популярные ответы по языку

Смотреть другие языки программирования