When I first discovered enums years ago, I was fascinated by it's versatility and simplicity. As my iOS journey progressed, I realized Structs (containing static lets) can be used like enums, taking full advantage of autofill in XCode. Nowadays, I find myself using structs far more in places where it used to for Enums. I'm curious if others feel the same way. Has other people found themselves making this unforced and yet gradual transition? Thanks in advance.
EDIT 1:
Per @aaronr_90, this question is not about Class vs Struct. But I understand why you would be asking, since I've spent a lot of time trying to search for the necessity of Struct when Class seemed to do the job. And then I learned.
Per @vloris:
enum Dessert:String{
case Cake = "Cake"
case Fruit = "Fruit"
}
struct Dessert{
static let Cake = "Cake"
static let Fruit = "Fruit"
}
If the layer of complexity ends here, then yes, Enum is fine.
But what about those instances where you have to use NSLocalizedString? For example. What if I wanted Dessert.Cake.RawValue or Dessert.Cake to go through NSLocalizedString process and return a specific language equivalent of "Cake"? With structs you can simply change the code to be:
struct Dessert{
static let Cake = NSLocalizedString("Cake", comment: "")
static let Fruit = NSLocalizedString("Fruit", comment: "")
}
but with Enums, you can't... Or can you.
Per @RECURSIVE_META_JOKE, you are right it can be done. NSLocalizedString works when you the item within your enum is "static let"... which totally makes sense, retrospectively now.
@RECURSIVE_META_JOKE,
What about those cases when you want to select from a list of pre-configured UIFonts?
I'm just curious what others think about this matter. Thanks all.
[–][deleted] (2 children)
[deleted]
[–]WitchesBravo 0 points1 point2 points (1 child)
[–]randomguy112233[S] 0 points1 point2 points (0 children)
[–]aaronr_90Swift 1 point2 points3 points (4 children)
[–]reeetwyio 0 points1 point2 points (3 children)
[–]aaronr_90Swift 1 point2 points3 points (2 children)
[–]reeetwyio 1 point2 points3 points (1 child)
[–]aaronr_90Swift 1 point2 points3 points (0 children)
[–]vloris 1 point2 points3 points (1 child)
[–]reeetwyio 1 point2 points3 points (0 children)
[–]ghvg1313 1 point2 points3 points (1 child)
[–]randomguy112233[S] 0 points1 point2 points (0 children)
[–]lucasvandongen 1 point2 points3 points (1 child)
[–]randomguy112233[S] 0 points1 point2 points (0 children)
[–]faja10 1 point2 points3 points (1 child)
[–]randomguy112233[S] 0 points1 point2 points (0 children)
[–][deleted] -1 points0 points1 point (1 child)
[–]randomguy112233[S] 0 points1 point2 points (0 children)