Swift Enum Raw Value Error: 'Raw value for enum case must be a literal'
The error 'Raw value for enum case must be a literal' occurs because enum raw values must be literal values, not method calls. To resolve this, define raw values directly as literals and create a computed property to calculate the scaled values. Here's an example:
enum fontSizeEm: CGFloat {
case small = 14
case standard = 16
case big = 18
case large = 20
var scaledValue: CGFloat {
return SCALE(rawValue)
}
}
In this code, the raw values are defined as literal values. The scaledValue computed property uses the SCALE method to calculate the scaled value based on the raw value. Access the scaled value using the scaledValue property on each enum case.
原文地址: https://www.cveoy.top/t/topic/hfZj 著作权归作者所有。请勿转载和采集!