迅捷cad_迅捷元组

迅捷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).

swift named tuple

上面的元组的类型是(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) = origin

newOrigin.0 = 1
newOrigin.1 = 5
newOrigin.2 = 10

print(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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值