Raw value for enum case must be a literal
That's because 1 << 0
isn't a literal. You can use a binary literal which is a literal and is allowed there:
enum GestureDirection:UInt {
case Up = 0b000
case Down = 0b001
case Left = 0b010
case Right = 0b100
}
Enums only support raw-value-literal
s which are either numeric-literal
(numbers) string-literal
(strings) or boolean-literal
(bool) per the language grammar.
Instead as a workaround and still give a good indication of what you're doing.
How to convert a computed value to a literal for enum initialization
You can only use literals for the raw values of type-backed enums.
To get this to work, you have to calculate the raw value of the calculation you're performing and paste that literal in as an approximation:
public enum ANGLE_TYPE : Double {
case DEGREES = 0.0174532925199433
case RADIANS = 1.0
}
The only other option is to not have a type-backed enum and manually provide the rawValue
property:
public enum ANGLE_TYPE {
case DEGREES, RADIANS
var rawValue: Double {
get {
switch self {
case .DEGREES:
return Double(CGFloat(M_PI / 180.0))
case .RADIANS:
return 1.0
}
}
}
}
This might make sense because this means you don't have the init(rawValue:Double)
initializer, which doesn't make a whole lot of sense in this case probably.
As a side note, this all caps thing is really unnecessary. I'd much prefer something more like this:
public enum AngleMeasureUnit {
case Degrees, Radians
}
Swift Enum implicit member expression with custom type
You can't use anything other than literals as raw values for enums, as the error suggests.
A literal is a fixed value represented in source code (so no method calls are allowed to construct it).
Swift has String literals (which allow String and Character raw values for enums), integer literals (which allow all the UInt/Int (8, 16, 32, 64) types) and floating point literals (Float, Double, ...). Types that can be constructed from a literal conform to the ExpressibleByIntegerLiteral
, ExpressibleByFloatLiteral
or ExpressibleByStringLiteral
protocols.
There are also array and dictionary literals but you cannot use them as raw values.
Otherwise, just use a computed property:
enum RequestButtons {
case cancel
var button: RequestButton {
switch self {
case .cancel:
return RequestButton(...)
}
}
}
Swift enum cannot assign default raw value
In (Objective-)C an enumeration defines a set of named integer constants, and those need not be distinct:
enum {
A = 1,
B = 2,
C = 99,
Default = B
};
The cases of a Swift enum
represent mutually distinct values, and their raw values – if assigned – must be unique:
enum BestLetters: Int {
case a = 1
case b
case c
case `default` = 2 // Error: Raw value for enum case is not unique
}
On the other hand, enumerations in Swift are first-class types, so that you can define a static property for that purpose:
enum BestLetters {
case a
case b
case c
static let `default` = BestLetters.b
}
(This is also how Swift imports C enumerations with duplicate values, compare touchIDLockout deprecated in iOS 11.0.)
enum using string constants NSLinguisticTagNoun not allowed
The error message states that the raw value must be a string literal. This differs from a string. A string literal is a string specified directly in the program (between quotes), not a variable of type String. See the code below:
"abcdef" // A string literal
let myString: String = "abcdef" // A constant of type String initialized with a string literal
myString // Not a string literal
NSLinguisticTagNoun and NSLinguisticTagVerb are constants of type String, not string literals. The requirement for enum raw values to be literals is a limitation in the current version of Swift. To replicate the functionality you're aiming for, you'll have to do:
enum WordTypes: String
{
case Noun = "Noun"
case Verb = "Verb"
}
Raw value for enum case must be a literal
That's because 1 << 0
isn't a literal. You can use a binary literal which is a literal and is allowed there:
enum GestureDirection:UInt {
case Up = 0b000
case Down = 0b001
case Left = 0b010
case Right = 0b100
}
Enums only support raw-value-literal
s which are either numeric-literal
(numbers) string-literal
(strings) or boolean-literal
(bool) per the language grammar.
Instead as a workaround and still give a good indication of what you're doing.
Related Topics
iOS Game Center: Scores Not Showing on Leaderboard in Sandbox
Can't Pass Data Correctly to Modal Presentation Using Foreach and Coredata in Swiftui
How to Embed a Youtube Video into My App
How to Detect Apps First Launch in iOS
Checking for Multiple Asynchronous Responses from Alamofire and Swift
How to Change the Background Color of the Uialertcontroller
Swift Performselector:Withobject:Afterdelay: Is Unavailable
Firebase Cloud Messaging Doesn't Create Push Notifications But Gets Information
Wkwebview Content Loaded Function Never Get Called
Swift 2.0 Sorting Array of Objects by Property
How to Hide Status Bar in Uiimagepickercontroller
Swift Image Retrieving from Parse Sdk - Getting Crashed
How to Render a Uiview with Transparent Background on an Scnplane in Arkit
Combine Framework Serialize Async Operations
403 Error - Thats an Error. Error: Disallowed_Useragent