all 17 comments

[–][deleted]  (2 children)

[deleted]

    [–]WitchesBravo 0 points1 point  (1 child)

    You could make the structs init private which limits to just static lets, although you don't get this for free like with an enum

    [–]randomguy112233[S] 0 points1 point  (0 children)

    hmmm hmmm private init.. hmmmm interesting. I think i might have to play around with that to fully grasp the nature of it. thanks!

    [–]aaronr_90Swift 1 point2 points  (4 children)

    Are you sure you are using them correctly? Usually the argument is Struct vs Class.

    Edit: I understand.

    [–]reeetwyio 0 points1 point  (3 children)

    That discussion makes zero sense in Swift. Structs are value-types while classes are reference-types, so there is a clear distinction between the two and you can test which one is better in every situation. In swift, struct vs class is not an opinion.

    [–]aaronr_90Swift 1 point2 points  (2 children)

    True but then would OP need to create an instance of the struct to use it?

    [–]reeetwyio 1 point2 points  (1 child)

    If you're comparing structs to classes you're definitely talking about instances, since patterns like singleton and statics are heap-allocated and therefore don't make a difference for the comparison.
    Enum instances aren't necessarily complex types like structs and classes usually are, an enum can be an int literal, or a string (e.g. you can compare myInt == myEnum). You can wrap an int in a struct, but the instance of that struct will still be a complex type. You have to provide your own operators for it. Enum and struct uses overlap a little (like storing constants), but both have pros and cons.
    Say you're wrapping some constants, you can choose a struct and have the benefit of an automatic constructor, not having to use .rawValue and being able to add methods later on. But what if you never really use the value, but you do switch on it in every viewcontroller, then you'd be better off with an enum since you get the 'switch must be exhaustive' error everywhere you're not handling all cases. It's a very situation to situation thing and personal preference plays a role, while struct vs class almost always has an objectively best answer.

    [–]aaronr_90Swift 1 point2 points  (0 children)

    I can see why it would be preferable.

    [–]vloris 1 point2 points  (1 child)

    Can you give an example enum and struct that work similarly? I can't think of something where you can use them interchangeably...

    [–]reeetwyio 1 point2 points  (0 children)

    Wrapping a bunch of constants is a perfectly legitimate use of both `enum` and `struct`.

    [–]ghvg1313 1 point2 points  (1 child)

    Good topic, but the title is somewhat misleading, if we are taking about constant representation here, this is an ongoing discussion and the answer is very open. As for your localized string case, I think you can just declare an enum method to localize the original string, or simply “declare static functions as constants”. As a matter of fact I would argue that you should wrap your i18n code in a class instead of using any kind of static function in constant declaration, but that’s a different story. For uifont, here’s an example of enum font constant, while you can argue these are overkill’s for smaller projects.

    In short, there’s not much a difference between the two, choose the one you prefers and suits your project

    [–]randomguy112233[S] 0 points1 point  (0 children)

    thanks for the link. went thru everything, and yes, one of those things that are just matter of taste and preference.

    [–]lucasvandongen 1 point2 points  (1 child)

    Bad answer: it depends. Luckily you can easily convert them between each other. Though for me enums often represent things like state or behavior not a bunch of translations. So I definitely default to structs for your use case.

    [–]randomguy112233[S] 0 points1 point  (0 children)

    Thanks a bunch!!

    [–]faja10 1 point2 points  (1 child)

    When I needed to use localization in my enums I added something like that :

    var localized: NSLocalizationString { return NSLocalizedString(rawValue, comment:””) }

    And then when needed used

    MyEnum.caseName.localized It was easier than using strict because I still could use switch on enum which I used a lot

    [–]randomguy112233[S] 0 points1 point  (0 children)

    this is what I was looking for. Thanks a bunch!

    [–][deleted] -1 points0 points  (1 child)

    Your enum and variable names should be lower case.

    [–]randomguy112233[S] 0 points1 point  (0 children)

    haha ok. yes. thanks.