eskimo
eskimo

Reputation: 101

How to store data with a dynamic key, and an array of data?

So I'm working on an IRC client for iOS but I can't figure out how to store some of the data. Previously I've mostly been a web dev, so in php I can just make an array like $channels[$dynamic_channel_name][$array_of_users].

I can't seem to find any way of doing this in swift. So what I'm trying to do is have an array of channels with a sub array of users for each channel.

I"m guessing this isn't how things are done for swift? The dynamic channel name is whats making everything I find on google useless, so hopefully someone here can drop a knowledge bomb.

Thanks in advance.

Upvotes: 0

Views: 169

Answers (1)

Dale Myers
Dale Myers

Reputation: 2821

PHP apparently lets you use a string as an index for an "array". In Swift this is a different data structure called a dictionary. You can create a dictionary like this:

let channels = [String: [User]]

This creates a variable named channels which contains a dictionary which maps a key which is a String to an array of Users (you might not have created this type, so it might just be an array of Strings for example.

You can then access similarly to before:

let users = channels[dynamic_channel_name]

The variable users now contains a list of your users.

Upvotes: 2

Related Questions