Tuple vs Dictionary differences

前端 未结 6 1214
悲&欢浪女
悲&欢浪女 2021-01-30 10:56

Can someone please explain what the major differences there are between Tuples and Dictionaries are and when to use which in Swift?

6条回答
  •  忘了有多久
    2021-01-30 11:40

    A dictionary is made up of key-value sets. A tuple is made for passing grouped values.

    Dictionaries:

    A dictionary is a container that stores multiple values of the same type. Each value is associated with a unique key, which acts as an identifier for that value within the dictionary.

    A dictionary should be used for creating lists of associated objects. An example use would be a dictionary of players and their scores:

    var scoreDictionary = ["Alice" : 100, "Bob" : 700]
    

    Tuples:

    Tuples group multiple values into a single compound value.

    A tuple should be used for passing groups of values. They are similar to arrays, but are fixed-length and immutable. An example use might be a tuple representing a 3-dimensional point:

    var myPoint = (10, 12, 14)
    

    As you can see there are many cases in which you would use a dictionary and many cases in which you would use a tuple. Each has its own specific purpose.

提交回复
热议问题