0

I'm following this tutorial on creating a collapsible TableView:

https://www.youtube.com/watch?v=Q8k9E1gQ_qg

He's loading a String array of names into the tableview like so:

struct ExpandableNames {
    
    var isExpanded: Bool
    let names: [String]
    
}

var twoDimensionalArray = [
        ExpandableNames(isExpanded: true, names: ["Amy", "Bill", "Zack", "Steve", "Jack", "Jill", "Mary"]),
        ExpandableNames(isExpanded: true, names: ["Carl", "Chris", "Christina", "Cameron"]),
        ExpandableNames(isExpanded: true, names: ["David", "Dan"]),
        ExpandableNames(isExpanded: true, names: ["Patrick", "Patty"]),
    ]

I would instead like to create the String array of names from my Codable User model's "name" variable:

 struct User: Codable {
        //var id: Int
        var user_id: Int
        var name: String
        
        static func endpointForUsers() -> String {
            return "users.php"
          
        }
    }

EDIT: In short... I have a [User] and I want a [String] made up of the name properties from the [User] array.

Could you please explain how I can then load all the names from my User codable as a String array? (Like the one in the tutorial.)

Thanks in advance!

6
  • 2
    It's not quite clear what you're asking. Do you have a [User] and you want [String] made up just of the name properties from the [User] array? Commented Jun 5, 2024 at 16:55
  • Well I guess it IS clear then! :) Yes. That's exactly what I want. Commented Jun 5, 2024 at 18:02
  • Have you heard about map? What exactly is the hard part here? — And why does it matter that User is Codable? Commented Jun 5, 2024 at 18:07
  • It probably doesn't in this scenario. But I just make all of my struct Arrays Codable because of how I use them elsewhere in the app. I'm not sure how I would use map in this scenario. I'm probably just overthinking this because I've been at it all day and nothing has worked. If you write it as an answer, and it works, I'll absolutely accept it right away. Commented Jun 5, 2024 at 18:13
  • "I have a [User] and I want a [String] made up of the name properties from the [User] array" namesList = usersList.map(\.name) Commented Jun 5, 2024 at 18:20

1 Answer 1

0

If you have an array of User objects and want to convert it to an array of Strings from the name properties of each User you can use the map() function.

Assuming you have 2 arrays of User objects:

struct User: Codable {
        //var id: Int
        var user_id: Int
        var name: String
        
        static func endpointForUsers() -> String {
            return "users.php"
          
        }
    }
}

func userNames(_ users: [User]) -> [String] {
    return users.map { $0.name }
}

let users1: [User] = // Fetch users from a JSON file, a network call, or whatever
let users2: [User] = // Fetch another array of users from somewhere.

// MARK: - new code here
var twoDimensionalArray = [
        // Create an ExpandableNames with the users from users1
        ExpandableNames(isExpanded: true, names: userNames(users1)),

        // Create an ExpandableNames with the users from users2
        ExpandableNames(isExpanded: true, names: userNames(users2)) ]

(I wrote my answer in the form of a function for clarity, but given that this is a single line of code, it's probably cleaner and simpler to just use the map statement inline in your code.)

Sign up to request clarification or add additional context in comments.

3 Comments

Sorry, I'm sure you've answered the question but I'm completely fried and just not getting it. So what would I put for "names: " under: ExpandableNames(isExpanded: true, names: ???)
The sample code in your question creates 4 separate ExpandableNames structs. Each one of those would need an array of User. Where would those array of User structs come from?
Your question isn't very clear. I edited my answer to show a somewhat more complete example. (I'm not going to watch the YouTube video to learn the context of your question. Sorry.)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.