Question regarding Arrays

I have a few questions regarding arrays vs tuples

  1. In the lesson I am following, the author states that one of the differences between a tuple and an array is that the data type of a tuple's values cannot be changed. This suggests that the data type can be changed for an array. My question is, how does one go about changing the data type of an array's elements?

  2. Also, am I correct in believing an array's values can only be of the same shared data type?

  3. Finally, why can I label a tuple's values like so (note the labels for the tuple are not strings/enclosed in double quotes:
    var myHensAges = (coco: 4, lolo: 2, rocky: 9)
    But when I try to name my array's elements in the same manner (i.e., without double quotes around each label) like so:
    var studentStats = [age: 52, year: 4, height: 72]
    I get an error message in Xcode: "Cannot find age/year/height in scope"
    This error message only goes away when I change each label to a string. Can someone please explain why this is necessary for an array?

  1. I'm not sure what the author is referring to there, tbh. The big differences between Arrays and tuples are that tuples can have more than one different type in them, and tuples cannot change size/layout. Neither Arrays nor tuples have mutable element types. Could you maybe post the exact text you're referring to?
  2. That's correct. That type could be Any, though.
  3. This is essentially for historical reasons: in the early days of Swift, tuples and function arguments were in many ways the same thing, so having labeled function arguments meant also having labeled tuples. If you want a collection where you can refer to elements by names like this, you can use a Dictionary instead. If you need both lookup by name and lookup by position, you can use OrderedDictionary from the Swift Collections package.

Thank you, this is the lesson I'm going through: Tuples – Swift in Sixty Seconds - YouTube

Regarding #3, this means then that the labels for an array must be strings (i.e., enclosed in double quotes) if I wish to access the array by an element's name? (I assume the answer is yes since I got an error message in Xcode when I tried to do that without enclosing the text in double quotes) :slight_smile:

1 Like

Arrays cannot have labels at all. Did you mean to say Dictionary?

1 Like

The "change" is something that happens in the coding process. Not at runtime. I.e. A tuple can use different types, but it doesn't need to. An Array has no choice.

(If that really was the wording, it's not good wording, but it would be accurate enough to make sense of.)

[Int](mirrorChildValuesOf: (1, "🤡")) // nil
[Int](mirrorChildValuesOf: (1, 1)) // [1, 1]

public extension Array {
  /// Create an `Array` if `subject's` values are all of one type.
  /// - Note: Useful for converting tuples to `Array`s.
  init?<Subject>(mirrorChildValuesOf subject: Subject) {
    guard let array = Mirror(reflecting: subject)\.value) as? Self
    else { return nil }

    self = array

Yikes, okay, I confused the two (dictionary and array). I know the answer now, too, thank you!

1 Like

Because of the labels, what you have written there is a Dictionary literal, not an Array literal.