0

I'm trying to figure out how to create a structure with multidimensional array.

I got it working for 1D array:

struct example1 {

var user: [String] 

}

but when I try to create an array with String, String, Int and a Bool it gives me "Expected ']' in array type" error

struct example2 {

var user: [[String], [String], [Int],[Bool]]

}

How to create empty multidimensional array that would work in structure like mine?

6
  • What is the purpose of that data model? Commented Apr 13, 2020 at 17:01
  • It's for an exercise where I'm supposed to create a struct that'd hold user data like name, email, age and a true/false bool. Commented Apr 13, 2020 at 17:05
  • Then use a struct rather than multiple arrays. Commented Apr 13, 2020 at 17:08
  • 1
    Then you should use another struct for this. Commented Apr 13, 2020 at 17:08
  • You're trying to make do with the tools you know (arrays). While I can appreciate that, it's just not the right tool for the job. You need a User struct, with properties you want (my guess is probably firstName: String, lastName: String, age: Int and some other boolean). You can then make an array of User structs. Commented Apr 13, 2020 at 17:09

1 Answer 1

0

Multiple arrays are bad practice for several reasons.

Create a struct User, each instance holds all information of one user

struct User {

    let name : String
    let email : String
    let age : Int
    let someBoolean : Bool
}

Declare the array

var users = [User]()

and add an item

let newUser = User(name: "Foo", email: "[email protected]", age: 12, someBoolean: false)
users.append(newUser)
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. That's exactly what I wanted to achieve! Why are multidimensional arrays not recommended?
Imagine you have to move one user from index 2 to index 6. Multiple arrays will drive you crazy.
@onesadlittleboy Among other things, Swift's arrays always have elements of a single type, such as Int, or String. If you want multiple types, then you need to use a more general type like a protocol or a superclass. The most general type is Any (which matches values of literally any type, be it Int, or String, or anything else), and you'll often see [Any], particularly working with unannoted Objective C APIs. The issue is that more general types achieve their generality by specificity less requirements. Any is a protocol with literally no requirements. ...
There's really nothing you can do with a value of type any Any. You can pass it around (such as to print, whose parameter has type Any), but that's about it. Doing anything useful at all with it requires you down cast it to the particular type, such as Int or String or whatever. For example, you can't do let x: Any = "Some string"; x.uppercased, because uppercased doesn't exist on Any. Using arrays of Any just devolves into needing to do casts all over the place, like (x as! String).uppercased. Structs solve this by allowing different types to coexist in a single struct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.