Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Generally clarification

Hello, I am trying to clarifiy a few terminologies in Swift. Array, Dictionary, and Tuples
They all seem to do the same thing.
Array
declaration:
var cardNames: [String] = ["Jack", "Queen", "King"]
to access the value of a specific field:
print(cardNames[0]), print(cardNames[1]), or print(cardNames[2])

Dictionary on the other had must be name: value pairs
declaration:
var cards = ["ace" : 1, "Jack" : 11, "Queen" : 12, "King" : 13]
to access the value of a specific field:
print(cards["ace"]!)...print(cards["King"]!)

Tuples
//UN-Named Tuple
var mixTypes = ("Anna", 40)

Optionally you can have named tuples
//Named Tuple
var mixTypes2 = (name: "Anna", age: 40, height: 5.7)
print(mixTypes2.0, "is", mixTypes2.1, "years old and", mixTypes2.2, "tall")

My questions are can an array have mixed datatype? What about Dictionary?
if tuples can be named, how is it different from Dictionary? for that matter an array can be named as well, how is it different from Dictionary and Tuples?


Thanks

Replies

  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,687 @ @ @ @ @ @ @ @
    I'm not quite sure why you say they are all the same.

    An array is a list of items that are accessed by index. You don't access data by name in an array.

    A dictionary is key-value pairs. You don't access data by index in a dictionary. And you cannot omit the keys/names.

    Tuples are sort of a hybrid. It is primarily a list of data, and you can access items by index. But as you note, you can optionally name the fields, and then access data by name. So you have both options. Arrays and dictionaries cannot do this.

    As far as mixed data types, not in the Objective-C sense, no. But you can declare that your contents are (for example) AnyObject, in which case you could put anything in there.
    Professional iOS App Development. Available for hire.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn

    BTIKit | BTICoreDataKit | SlickShopper 2 | Leave a PayPal donation
Sign In or Register to comment.