I have a question about xcode7.x Swift. (Error while using Structure and Array)

Asked 2 years ago, Updated 2 years ago, 31 views

Hello, I am a student who is making an application using swift. I'll ask you a question right away.

The current situation is that some initial values are given after a structure declaration in one table view We're trying to get the structure that gave us the initial value back together and make it into an array. There is no problem with the playground, but if you write it on the project, an error occurs like the picture shown below. I looked it up on the Internet and it seems to be okay if you write it inside the override func... I want to use this array as a global variable. Is there any way?

swift ios

2022-09-22 15:44

1 Answers

I think you can initialize the value in the viewDidLoad() method.

struct Zone {
        var name : String
        var controller : [controllerType]

        enum controllerType {
            case LG_TV
            var typeTitle : String {
                get {
                    let titleString : String
                    switch self {
                    case .LG_TV:
                        titleString = "LG TV"

                    }
                    return titleString
                }
            }
        }
    }

    var Room1 = Zone(name : "ROOM1", controller : [.LG_TV, .LG_TV, .LG_TV])
    var Room2 = Zone(name : "ROOM2", controller : [])
    var Room3 = Zone(name : "ROOM3", controller : [])

    var Zones : [Zone] = [] 

    override func viewDidLoad() {
        super.viewDidLoad()

        Zones = [Room1, Room2, Room3]

        print(Zones)  
        //or 
        Zones.append(Room1)
        Zones.append(Room2)
        Zones.append(Room3)

        print(Zones)
    }

If I misunderstand the question or if there is anything wrong, please leave a request for editing or comment :)

+++++++++++++ Add++++++++++++++++++++


2022-09-22 15:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.