Swift Concept Questions - Variables and SwiftUI Views

  • Is there any other benefit of using constant over variables?
    I have heard my colleagues saying that if we declare a constant swift assigns it a fixed memory, and that is the benefit of declaring constant over variables that won’t change the value. But I have also gone through the this thread , and it specifies that we use it because Xcode will prompt us in case if we are trying to change the value of constant. Is that the only reason that we should go for?

  • Why SwiftUI Views are Structs?
    Is it because structs are lightweight and classes aren’t? Or is it because classes are of reference types and structs are of variable types.

The place for a constant (a let) is where you need to store something that never changes or does not need to be changed.
If you try to change a constant (you might have considered initially that it is going to be a Constant and later on decide that it should change), Xcode will tell you that you cannot do that in which case you need to make it a var.

Having constants is particularly useful where you want to store something that will be used in multiple places (such as a String) and this can avoid making mistakes in retyping that same String in multiple places.

This is probably the best explanation:

1 Like

Technically, since View is a protocol and not actually a struct, you can have Views that are classes or enums, but they aren’t as common as structs.

A simple example of an enum being a View:

enum EnumView: View {
    
    case textView
    case imageView
    //using associated values to supply additional information
    case labelView(String, String)
    
    var body: some View {
        switch self {
        case .textView:
            Text("Hello world")
        case .imageView:
            Image(systemName: "person")
        case let .labelView(text, image):
            Label(text, systemImage: image)
        }
    }
}

struct MultiView: View {
    var body: some View {
        NavigationView {
            VStack(spacing: 20) {
                NavigationLink(destination: EnumView.textView) {
                    Label("Text View", systemImage: "arrow.forward")
                }
                NavigationLink(destination: EnumView.imageView) {
                    Label("Image View", systemImage: "arrow.forward")
                }
                NavigationLink(destination: EnumView.labelView("Square", "square")) {
                    Label("Label View", systemImage: "arrow.forward")
                }
            }
        }
    }
}
1 Like

Thank you so much @Chris_Parker and @roosterboy
Just a clarification, so there is no such memory advantage right for using constant and variable, like if we declare a constant swift assigns a fixed memory based on the value we are assigning?

To be honest with you it’s an issue that I don’t think you need to consider. I mean we are not talking about an 8 bit Atari with 64K of memory.

1 Like

Thanks :smiley:

Structs are value types, not variable types

I’m not exactly sure about a memory advantage, yeah I wouldn’t worry about it at all, as Chris Parker said

But also the biggest advantage is preventing bugs. Making something a constant means it cannot change. When it’s a variable, it can change and if you made something a variable when it should’ve been a constant, you could possibly have bugs because that value is being changed somewhere, when it shouldn’t be. And it could be hard to track down

1 Like

Thank you!! :smiley:

1 Like