Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using 720p, 1080p as enum in swift

Tags:

enums

swift

I want to use 720p, 1080p as enums in swift. However, I can't. I got an error saying "Expected a digit after integer literal prefix"

enum ASResolution {
    case lowResolution
    case 720p
    case 1080p
    caee highResolution
}

What should I do?

I revised my code as below:

enum ASResolution:Int {
    case low = 1
    case HD = 720
    case fullHD = 1080
    case high = 2000
}
like image 658
Owen Zhao Avatar asked Dec 04 '25 04:12

Owen Zhao


1 Answers

There isn't much you can do if you want to keep those names. An enum case is an identifier, and as you can see from the Language Reference there are restrictions for what you can use as the first character.

So, pretty much as you can't start a variable name with a digit (they are identifiers as well), you can't start an enum case with a digit.

The few options you have are:

  • prefix the identifier with a permitted character (such as the underscore)
  • completely change the identifier, using words (sevenTwoZeroP and oneZeroEightZeroP) or synonyms (HDReady and FullHD)
like image 73
Antonio Avatar answered Dec 07 '25 03:12

Antonio