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)
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
// MARK: Enumerations used in swift
let result = "failure"
let result2 = "failed"
let result3 = "fail"
enum Result {
case success
case failure
}
let res = Result.failure
print(res)
// 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)
}
}
}
enum Season {
case spring, summer, autumn, winter
}