元组与字典差异 [英] Tuple vs Dictionary differences

查看:123
本文介绍了元组与字典差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释一下元组和字典之间存在的主要区别是什么以及何时在Swift中使用哪些?

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

推荐答案

主要区别:




  • 如果你需要从方法中返回多个值,你可以使用元组。

  • 元组不需要像Dictionary这样的任何键值对。

  • 元组只能包含预定义数量的值,在字典中没有这样的限制。

  • 元组可以包含具有不同数据类型的不同值,字典一次只能包含数据类型值

  • 元组对于从函数返回多个值特别有用。字典可以用作模型对象。

  • Major difference:

    • If you need to return multiple values from a method you can use tuple.
    • Tuple won't need any key value pairs like Dictionary.
    • A tuple can contain only the predefined number of values, in dictionary there is no such limitation.
    • Tuple can contain different values with different datatype, dictionary can contain only on datatype value at a time
    • Tuples are particularly useful for returning multiple value from a function. Dictionary can be used as a model object.
    • 在命名元组中,我们为每个元素指定个别名称。

      In Named tuple we assign individual names to each elements.

      定义如下:

      let nameAndAge = (name:"Midhun", age:7)
      

      访问以下值:

      nameAndAge.name
      nameAndAge.age
      






      2未命名的元组



      在未命名的元组中,我们没有为其元素指定名称。


      2 Unnamed Tuple

      In unnamed tuple we don't specify the name for it's elements.

      定义如下:

      let nameAndAge = ("Midhun", 7)
      

      访问以下值:

      nameAndAge.0
      nameAndAge.1
      

      let (theName, thAge) = nameAndAge
      theName
      thAge
      






      参考:

      元组




      Reference:

      Tuple


      使用元组可以创建和传递值的分组。你
      可以使用一个元组将函数中的多个值作为单个
      复合值返回。

      Tuples enable you to create and pass around groupings of values. You can use a tuple to return multiple values from a function as a single compound value.

      你可以在 Swift编程语言中查看有关元组的更多信息

      You can check more about Tuple in Swift Programming Language


      字典是一个容器,它存储同一
      类型的多个值。每个值都与一个唯一键相关联,该词充当字典中该值的
      标识符

      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

      您可以检查有关字典的更多信息,请参见 Swift CollectionTypes

      You can check more about Dictionary in Swift CollectionTypes

      这篇关于元组与字典差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆