“Enum Swift” Kode Jawaban

Enum Swift

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

Tentukan 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

dapatkah Anda meneruskan enum sebagai parameter ke fungsi cepat

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

Nib enum Swift

// 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 Swift

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

Jawaban yang mirip dengan “Enum Swift”

Pertanyaan yang mirip dengan “Enum Swift”

Lebih banyak jawaban terkait untuk “Enum Swift” di Swift

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya