Swift学习之元组(Tuple)
定义
元组是由若干个类型的数据组成,组成元组的数据叫做元素,每个元素的类型都可以是任意的。
用法一
let tuples1 = ("Hello", "World", 2017)
//元组跟数组一样,其元素的角标是从0开始 可以用 tuple1.0 tuple1.1 tuple1.2进行取值
print("get first value \(tuples1.0), get the second value \(tuples1.1), get the third value \(tuples1.2)")
用法二
let tuples2 = (first:"Hello", second:"World", third:2017)
//此中用法的元组的取值可以用 tuple2.first tuple2.second tuple3.third
print("get the first value \(tuples2.first), get the second value \(tuples2.second), get the third value \(tuples2.third)")
用法三
//变量接收数值法
let (x, y, z) = ("Hello", "World", 2017);
print("get x is \(x) get y is \(y) get z is \(z)")
//用_取出某些元素的值忽略_对应的值
let (a, _, b) = ("Hello", "World", 2017);
print("get x is \(a) get z is \(b)")
元组值的修改
//此时获得的元组为不可变元组 不能对其元素进行修改
let tuples2 = (first:"Hello", second:"World", third:2017)
//此中用法的元组的取值可以用 tuple2.first tuple2.second tuple3.third
print("get the first value \(tuples2.first), get the second value \(tuples2.second), get the third value \(tuples2.third)")
//此时会报错
//tuples2.first = "test"; 应将tuple2前的let 改成var可将元组变成可变元组
//此时创建的元组为可变元组,其元素可以进行修改
var tuples2 = (first:"Hello", second:"World", third:2017)
//此中用法的元组的取值可以用 tuple2.first tuple2.second tuple3.third
var print("get the first value \(tuples2.first), get the second value \(tuples2.second), get the third value \(tuples2.third)")
tuples2.first = "test";
print("get the first value \(tuples2.first), get the second value \(tuples2.second), get the third value \(tuples2.third)")
元组的高级应用
定义一个可以交换一个数组中两个元素的函数
func swap<T>(_ nums: inout [T], _ p: Int, _ q: Int) {
(nums[p], nums[q]) = (nums[q], nums[p])
}
var array = ["Hello", "World", 2017] as [Any]
for value in array {
print(value);
}
swap(&array, 0, 2)
for value in array {
print(value);
}
Swift学习之元组(Tuple)的更多相关文章
- Swift中的元组tuple的用法
用途 tuple用于传递复合类型的数据,介于基础类型和类之间,复杂的数据通过类(或结构)存储,稍简单的通过元组. 元组是使用非常便利的利器,有必要整理一篇博文. 定义 使用括号(), 括号内以逗号分割 ...
- swift学习之元组
元组在oc中是没有的.在swift中是新加的,学oc数组概念时还在想既然能够存储同样类型的元素,那不同类型的元素有没有东西存储呢,答案非常悲伤,oc没有元组这个概念.只是swift中加入了这个东西,也 ...
- Python学习笔记(4)--数据结构之元组tuple
元组(tuple) 定义:tuple和list十分相似,但是tuple是不可变的,即不能修改tuple 初始化:(), ('a', ) , ('a', 'b') //当只有一个元素时,需加上逗号, ...
- 记录:swift学习笔记1-2
swift还在不断的更新做细微的调整,都说早起的鸟儿有虫吃,那么我们早点出发吧,趁着国内绝大多数的coder们还没有开始大范围普遍应用. 网上有些大神说:swift很简单!我不同意这个观点,假如你用h ...
- Swift学习笔记八
函数 Swift的函数语法非常独特,也提供了很高的灵活性和可读性.它可以充分表达从简单的无参数C风格函数到复杂的拥有局部变量和外部变量的OC风格的方法.参数可以有默认值,方便函数的调用.Swift中的 ...
- Swift学习笔记四
前面三篇笔记通过一些示例展示了Swift的一些特性,粗略地介绍了它的语法和特色,从这一篇笔记开始,将正式系统地介绍Swift的语法和特性了. Swift是一门为iOS和OSX开发准备的全新语言,但是它 ...
- Swift 学习- 07 -- 函数
// 函数 // 函数是一段完成特定任务的独立代码片段, 你可以通过给函数命名来标识某个函数的功能, 这个名字可以被用来在需要的时候'调用'这个函数来完成它的任务 // swift 统一的函数语法非常 ...
- Swift 学习- 06 -- 控制流
// 控制流 // swift 提供了多种控制流结构,包括可以多次执行的 while 循环,基于特定条件选择执行不同分支的 if, guard 和 switch 语句,还有控制流程跳转到其它代码位置的 ...
- Swift 学习- 02 -- 基础部分2
class NamedShape{ var numberOfSides: Int = 0 var name: String init(name: String) { self.name = name ...
随机推荐
- 摘抄自知乎的redis相关
1.知乎日报的基础数据和统计信息是用 Redis 存储的,这使得请求的平均响应时间能在 10ms 以下.其他数据仍然需要存放在另外的地方,其实完全用 Redis 也是可行的,主要的考量是内存占用.就使 ...
- javascript中的构造函数和继承
1.第一节 使用工厂模式创建一个构造函数CreatePerson function CreatePerson(name,sex){//构造函数:用于构造对象 可以说在js里类就是构造函数 //1.原料 ...
- javaScript高级程序设计笔记 2
Undefinde Null Boolean Number String 基本类型 Object 引用类型 只有引用类型才能动态的添加属性 赋值基本类型和引用类型也不相同,复制的基本类型的 ...
- java 单链表的实现
package liaobiao;//链表测试public class Node { private int value; private Node next; //存放下一个节点的指针 //构造方法 ...
- 远程调用其它站点并设置cookie
远程调用其它站点并设置cookie: 参考js var domainArray = [ {site:'g.com',action:'/b.do?c' } ,{site:'www.baidu.com', ...
- WPF编程-WPF体系结构
WPF简介 Windows Presentation Foundation(WPF)是微软新一代图形系统,运行在.NET Framework 3.0架构下,为用户界面.2D/3D 图形.文档和媒体提供 ...
- Python 编码错误的本质原因
转载自:https://foofish.net/python-unicode-error.html 不论你是有着多年经验的 Python 老司机还是刚入门 Python 不久的新贵,你一定遇到过Uni ...
- [leetcode-553-Optimal Division]
Given a list of positive integers, the adjacent integers will perform the float division. For exampl ...
- 【LeetCode】309. Best Time to Buy and Sell Stock with Cooldown
题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
- 【Android Developers Training】 72. 缩放一个视图
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...