Swift笔记(一)
2016-01-12 21:39
507 查看
我的主力博客:半亩方塘
1、In Swift, you can optionally use underscores to make larger numbers more human-readable. The quantity and placement
of the underscores is up to you.
2、
3、Here is how to access the data inside the tuple:
You can reference each item in the tuple by its position in the tuple, starting with zero. Swift allows you to name the
individual parts of a tuple, so you to be explicit about what each part represents. For example:
If you want to access multiple parts of the tuple at the same time, you can also use a shorthand syntax to make it easier:
The code is equivalent to the following:
If you want to ignore a certain element of the tuple, you can replace the corresponding part of the declaration with an
underscore.
This line of code only declares
The
can use the underscore throughout Swift to ignore a value.
4、Sometimes it's useful to check the inferred type of a variable or constant. You can do this in a playground by holding down the Option key
and clicking on the variable or constant's name.
1、In Swift, you can optionally use underscores to make larger numbers more human-readable. The quantity and placement
of the underscores is up to you.
var variableNumber: Int = 1_000_000
2、
var integer: Int = 100 var decimal: Double = 12.5 integer = decimal // wrong integer = Int(decimal) // right
3、Here is how to access the data inside the tuple:
let coordinates: (Int, Int) = (2, 3) let x: Int = coordinates.0 let y: Int = coordinates.1
You can reference each item in the tuple by its position in the tuple, starting with zero. Swift allows you to name the
individual parts of a tuple, so you to be explicit about what each part represents. For example:
let coordinatesNamed: (x: Int, y: Int) = (2, 3) let x: Int = coordinatesNamed.x let y: Int = coordinatesNamed.y
If you want to access multiple parts of the tuple at the same time, you can also use a shorthand syntax to make it easier:
let coordinates3D: (x: Int, y: Int, z: Int) = (2, 3, 1) let (x, y, z) = coordinates3D
The code is equivalent to the following:
let coordinates3D: (x: Int, y: Int, z: Int) = (2, 3, 1) let x = coordinates3D.x let y = coordinates3D.y let z = coordinates3D.z
If you want to ignore a certain element of the tuple, you can replace the corresponding part of the declaration with an
underscore.
let (x, y, _) = coordinates3D
This line of code only declares
xand
y.
The
_is special and simply means you are ignoring this part for now. You'll find that you
can use the underscore throughout Swift to ignore a value.
4、Sometimes it's useful to check the inferred type of a variable or constant. You can do this in a playground by holding down the Option key
and clicking on the variable or constant's name.
相关文章推荐
- Apple Swift学习教程
- 介绍 Fedora 上的 Swift
- Swift中实现点击、双击、捏、旋转、拖动、划动、长按手势的类和方法介绍
- Swift编程中的泛型解析
- Swift中定义二维数组的方法及遍历方法示例
- 简单分析Swift语言的一些基本特征
- Swift与C语言指针结合使用实例
- Swift心得笔记之控制流
- 用Swift构建一个简单的iOS邮件应用的方法
- 苹果公司推出的新编程语言Swift简介和入门教程
- 探讨Swift数组和字典
- Swift中的指针操作和使用详细介绍
- Swift中使用正则表达式的一些方法
- Swift心得笔记之运算符
- Swift中使用可选类型完美解决占位问题
- Swift学习笔记之构造器重载
- Swift中的Access Control权限控制介绍
- Swift类型创建之自定义一个类型详解
- Swift教程之控制流详解