“Enume rapide” Réponses codées

Enume rapide

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

Définir l'énumération 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

Pouvez-vous passer une énumération en tant que paramètre à une fonction 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

Enume rapide

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

Réponses similaires à “Enume rapide”

Questions similaires à “Enume rapide”

Plus de réponses similaires à “Enume rapide” dans Swift

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code