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?
Also, am I correct in believing an array's values can only be of the same shared data type?
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?
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?
That's correct. That type could be Any, though.
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.
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)