迅捷cad

Swift tuple is a container to hold multiple values. Swift Arrays are largely homogeneous data types. Same isn’t the case with tuples. Let’s get started by launching the Swift Playground in XCode.

Swift元组是容纳多个值的容器。 Swift数组在很大程度上是同类数据类型。 元组不是这样。 让我们开始使用XCode启动Swift Playground。

迅捷元组 (Swift Tuple)

Tuples can hold zero or more values. The values need not be of the same type. Tuples in Swift are like mini structs and should be used for storing temporary values only.
Tuples are handy when we have to return multiple values from a function.

元组可以容纳零个或多个值。 这些值不必是同一类型。 Swift中的元组就像迷你结构 ,应仅用于存储临时值。
当我们必须从一个函数返回多个值时,元组很方便。

创建一个元组 (Creating a Tuple)

Values in a tuple are comma separated as shown below.

元组中的值以逗号分隔,如下所示。

var firstTuple = ("Anupam Chugh","JournalDev.com")
print(firstTuple.0)
print(firstTuple.1)
print(type(of: firstTuple))
  • Creating a tuple is similar to defining variables. All we need to do is set the values separated by commas in parentheses.创建元组类似于定义变量。 我们需要做的就是设置括号中用逗号分隔的值。
  • Type of the tuple is inferred from the definition itself.元组的类型是从定义本身推断出来的。
  • The type of a Swift Tuple is the type of all of its values.Swift元组的类型是其所有值的类型。
  • To access the values from the tuple we need to use the dot syntax followed by the index of the value.要从元组访问值,我们需要使用点语法和值的索引。
  • Instead of using indexes to access the elements, we can set names to the tuple elements.可以使用名称来设置元组元素,而不是使用索引来访问元素。

In order to define an empty tuple you just need to do : var voidTuple = (). It has the type (Void)

为了定义一个空的元组,您只需要做: var voidTuple = () 。 它具有类型(Void)

We can create tuples by defining the types as shown below:

我们可以通过定义如下所示的类型来创建元组:

var tuple : (Int, Bool) = (2,false)

Swift元组元素名称 (Swift Tuple Element Names)

We can name each element of the tuple similar to how parameters are named in Swift Functions

我们可以命名元组的每个元素,类似于在Swift函数中如何命名参数

var namedTuple = (blogger: "Anupam Chugh",tutorial: "Swift Tuples", year : 2018)
print(namedTuple.blogger)
print(namedTuple.tutorial)
print(namedTuple.year)

The type of the above tuple is (String, String, Int).

上面的元组的类型是(String, String, Int)

分解Swift元组 (Decomposing Swift Tuple)

We can retrieve and set values from a tuple to variables and constants. This is known as decomposing tuples.

我们可以从元组中检索值并将其设置为变量和常量。 这称为分解元组。

var (name, website) = firstTuple
let (b, t, y) = namedTuple
let(_,websiteName) = firstTuple
  • Setting an underscore means you’re ignoring that value from the tuple.设置下划线意味着您将忽略元组中的该值。
  • This is a good way to define multiple variables and constants from tuples.这是从元组定义多个变量和常量的好方法。
  • The constants defined in the let statement can’t be modified.let语句中定义的常量无法修改。

Using the above method we can also assign multiple values at the same time.

使用上述方法,我们还可以同时分配多个值。

(name, website) = ("Pankaj", "JournalDev.com")

使用Swift Tuples交换两个元素 (Swapping two elements using Swift Tuples)

Provided they are of the same type, we can swap the values in two variables using a tuple

假设它们具有相同的类型,我们可以使用元组交换两个变量中的值

var i = 10
var j = 20
(i,j) = (j,i)
print("i is \(i) j is \(j)") //prints i is 20 j is 10

Swift元组是值类型 (Swift Tuples are Value Types)

Tuples in Swift are value types and not reference types.

Swift中的元组是值类型,而不是引用类型。

var origin = (x: 0,y: 0,z: 0)
var newOrigin:(Int,Int,Int) = originnewOrigin.0 = 1
newOrigin.1 = 5
newOrigin.2 = 10print(origin) // "(x: 0, y: 0, z: 0)\n"
print(newOrigin) // "(1, 5, 10)\n"

As it is evident from the above example, values changed in the new tuple have no impact over the earlier one from which they were copied.

从上面的示例可以明显看出,新元组中更改的值对从中复制值的较早值没有影响。

Note: Values present in a let tuple cannot be modified.

注意let元组中的值不能修改。

另一个元组中的Swift元组 (Swift Tuple inside another tuple)

Tuples are compound types. One Tuple can be nested inside another.

元组是复合类型。 一个元组可以嵌套在另一个中。

var nestedTuple = (("Swift","Swift tuples tutorial"),("Android","Android Volley Tutorial"))
nestedTuple.0.1 //Swift tuples tutorial

从函数返回多个值 (Returning Multiple Values From a Function)

Using Swift tuple we can return multiple values from a function as shown below.

使用Swift元组,我们可以从一个函数返回多个值,如下所示。

func sumOfTwo(first a: Int, second b: Int) -> (Int,Int,Int)
{return(a,b,a+b)
}var returnedTuple = sumOfTwo(first: 2, second: 3)
returnedTuple.2 //5

To set the element names in the returned tuple, we need to explicitly set them in the tuple type definition itself.

要在返回的元组中设置元素名称,我们需要在元组类型定义本身中显式设置它们。

var returnedTuple : (first: Int, second: Int, sum: Int) = sumOfTwo(first: 2, second: 3)
print(returnTuple.first) //3
print(returnTuple.second) //3
print(returnTuple.sum) //5

That’s all for a quick roundup on the swift tuple.

这就是对快速元组的快速汇总。

翻译自: https://www.journaldev.com/19515/swift-tuple

迅捷cad

迅捷cad_迅捷元组相关推荐

  1. 迅捷cad_迅捷功能

    迅捷cad In this swift function tutorial, we'll be looking at how functions are defined and used in Swi ...

  2. 迅捷cad_迅捷数组

    迅捷cad In this tutorial, we'll be discussing about Swift Array. If you don't know how Optional in Swi ...

  3. 迅捷cad_迅捷泛型

    迅捷cad In this tutorial, we'll be discussing Generics in Swift. We'll be seeing how we can extend Gen ...

  4. 迅捷cad_迅捷协议

    迅捷cad Continuing with our series of Swift tutorials, we'll be discussing and playing around with ano ...

  5. 迅捷cad_迅捷属性

    迅捷cad In this tutorial, we'll be discussing about Swift Properties. Properties are an important part ...

  6. 迅捷cad_迅捷结构

    迅捷cad In this tutorial, we'll be discussing an important building block in Swift, namely Structures. ...

  7. 迅捷cad_迅捷套装

    迅捷cad In this tutorial, we'll be discussing the use cases of Swift Set. We'll look at the basic synt ...

  8. 一种方便快速的在线制作流程图的软件-迅捷画图

    个人中心 - 迅捷画图迅捷画图网提供在线绘图与文件在线存储功能,您可随时随地进入个人中心查看和下载创建过的绘图文件,并且不用担心文件丢失问题.https://huatu.98youxi.com/hom ...

  9. 如何在iOS或macOS上检查活动的Internet连接?

    我想检查一下我是否在使用Cocoa Touch库的iOS上或在使用Cocoa库的macOS上建立了Internet连接. 我想出了一种使用NSURL做到这一点的方法. 我这样做的方式似乎有点不可靠(因 ...

最新文章

  1. Windows Azure AppFabric概述
  2. eclipse导出jar包
  3. 国外JAVA与IT技术网站地址
  4. Dapr微服务应用开发系列1:环境配置
  5. hdu4911 Inversion-归并排序
  6. LeetCode 1473. 给房子涂色 III(DP)
  7. nginx ci index.php,CI在Nginx服务器上rewrite去掉index.php例子
  8. linux驱动编写(nand flash驱动)
  9. 按值传递和按引用传递的区别_c++按值、地址、引用传递参数
  10. 使用ZooKeeper ACL特性进行znode控制
  11. 数字臧品系统开发运行逻辑数字臧品系统开发源码搭建流程
  12. 计算机网络体系结构(详图)
  13. flash动画短片制作常用表现方法与技巧(精华)
  14. 钉钉微应用调用支付宝JSAPI进行支付
  15. Ionic for Angular 环境搭建
  16. 文件查找工具Everything的使用技巧
  17. CCF CSP 201809-1 卖菜 题解
  18. 《卓有成效的管理者》——学习心得(四)
  19. 【linux】一篇全面的linux软件包管理的总结
  20. 在pycharm中使用arcpy

热门文章

  1. 纪念宾得4000万象素的120数码相机645D推出两周年
  2. [转载] python staticmethod有什么意义_Python 中的 classmethod 和 staticmethod 有什么具体用途
  3. [转载] Unicode文本排序和Unicode数据库
  4. [转载] java中对象作为参数传递给一个方法,到底是值传递,还是引用传递
  5. 关于 Object.prototype.toString.call() 方法
  6. DT大数据 scala for查询
  7. XML DataBase之Xindice(二)
  8. 如何增加虚拟机ubuntu的硬盘
  9. python email模块详解_Python使用email模块对邮件进行编码和解码的实例教程
  10. robot连接mysql_Robotframework使用自写库连接mysql数据库