"Where and how should I write the constants that can be used throughout the application?

Asked 2 years ago, Updated 2 years ago, 25 views

Here's a quick way to come up with
·Use ENUM
·Create a Define class
That's it.

What should I do?

swift

2022-09-29 21:38

2 Answers

If you want to create a special class, I think you should use structure.

Swift's class cannot have a class constant to hold the value, but it is possible if it is struct.

// Compile without problems
US>structure ConstStruct{
    static let nameMax = 32
    static let imageMargin —CGFloat=10.0
    staticlet defaultName="NO NAME"
}

// get a compilation error
classConstClass{
    static let nameMax = 32
    static let imageMargin —CGFloat=10.0
    staticlet defaultName="NO NAME"
}


2022-09-29 21:38

"If you give ""let"", I think you can treat anything as a constant.
If you don't forget the style of C language, you may not be able to feel it right.

As for "where", I think it would be independent of the implementation of classes, structures, etc.


2022-09-29 21:38

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.