Home > Software design >  How can I create data structure with multiple values in Swift?
How can I create data structure with multiple values in Swift?

Time:06-14

I'm learning Swift and I'm wondering how can I create a data structure with multiple values and pass descriptions values from UITableViewController to another viewController? I have tried like this


   struct faculty {
        var name = String()
        var descriptions = (String)[]
   }
   let faculties = [name: "Faculties", description: ["Study1", "Study2"]]

I have successfully managed to list an array ["Test1", "Test2"] in tableView.

CodePudding user response:

There are a couple of issues

  • An empty string array is [String]().
  • description is not equal to descriptions.
  • An instance must be created with Type(parameter1:parameter2:).
  • And structs are supposed to be named with starting capital letter.

struct Faculty {
     var name = String()
     var descriptions = [String]()
}

let faculties = [Faculty(name: "Faculties", descriptions: ["Study1", "Study2"])]

However default values are not needed. This is also valid

struct Faculty {
     let name : String
     var descriptions : [String]
}
  • Related