add code highlight

This commit is contained in:
梁杰
2014-08-25 21:14:56 +08:00
parent e0388c788a
commit 86a6802d5d
42 changed files with 209 additions and 40 deletions

View File

@ -1,6 +1,11 @@
> 翻译:[numbbbbb](https://github.com/numbbbbb)
> 校对:[shinyzhu](https://github.com/shinyzhu), [stanzhai](https://github.com/stanzhai)
<link rel="stylesheet" href="../assets/css/xcode.css">
<script src="../assets/js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
# Swift 初见
---

View File

@ -1,6 +1,10 @@
> 翻译:[成都老码团队翻译组-Ayra](http://weibo.com/littlekok/)
> 校对:[成都老码团队翻译组-Oberyn](http://weibo.com/u/5241713117)
<link rel="stylesheet" href="../assets/css/xcode.css">
<script src="../assets/js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
# Swift 版本历史记录
---

View File

@ -1,6 +1,10 @@
> 翻译:[numbbbbb](https://github.com/numbbbbb), [lyuka](https://github.com/lyuka), [JaySurplus](https://github.com/JaySurplus)
> 校对:[lslxdx](https://github.com/lslxdx)
<link rel="stylesheet" href="../assets/css/xcode.css">
<script src="../assets/js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
# 基础部分
-----------------

View File

@ -3,6 +3,10 @@
> 校对:[EvilCome](https://github.com/Evilcome)
> 最终校对:[老码团队翻译组-Tyrion](http://weibo.com/u/5241713117)
<link rel="stylesheet" href="../assets/css/xcode.css">
<script src="../assets/js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
# 基本运算符
-----------------
@ -13,7 +17,7 @@
- [算术运算符](#arithmetic_operators)
- [组合赋值运算符Compound Assignment Operators](#compound_assignment_operators)
- [比较运算符](#comparison_operators)
- [三目运算符Ternary Conditional Operator](#ternary_conditional_operator)
- [三目运算符Ternary Conditional Operator](#ternary_conditional_operator)
- [空合运算符](#nil_coalescing_operator)
- [区间运算符](#range_operators)
- [逻辑运算符](#logical_operators)
@ -286,46 +290,46 @@ Swift 也提供恒等`===`和不恒等`!==`这两个比较符来判断两个对
rowHeight = rowHeight + 20
}
// rowHeight 现在是 90
第一段代码例子使用了三目运算,所以一行代码就能让我们得到正确答案。这比第二段代码简洁得多,无需将`rowHeight`定义成变量,因为它的值无需在`if`语句中改变。
三目运算提供有效率且便捷的方式来表达二选一的选择。需要注意的事,过度使用三目运算符会使简洁的代码变的难懂。我们应避免在一个组合语句中使用多个三目运算符。
<a name="nil_coalescing_operator"></a>
## 空合运算符(Nil Coalescing Operator)
空合运算符(`a ?? b`)将对可选类型`a`进行空判断,如果`a`包含一个值就进行解封,否则就返回一个默认值`b`.这个运算符有两个条件:
- 表达式`a`必须是Optional类型
- 默认值`b`的类型必须要和`a`存储值的类型保持一致
空合并运算符是对以下代码的简短表达方法
a != nil ? a! : b
上述代码使用了三目运算符。当可选类型`a`的值不为空时,进行强制解封(`a!`)访问`a`中值,反之当`a`中值为空时返回默认值b。无疑空合运算符(`??`)提供了一种更为优雅的方式去封装条件判断和解封两种行为,显得简洁以及更具可读性。
> 注意:
如果`a`为非空值(`non-nil`),那么值`b`将不会被估值。这也就是所谓的短路求值。
下文例子采用空合并运算符,实现了在默认颜色名和可选自定义颜色名之间抉择:
let defaultColorName = "red"
var userDefinedColorName:String? //默认值为nil
var colorNameToUse = userDefinedColorName ?? defaultColorName
//userDefinedColorName的值为空 所以colorNameToUse的值为`red`
`userDefinedColorName`变量被定义为一个可选字符串类型默认值为nil。由于`userDefinedColorName`是一个可选类型,我们可以使用空合运算符去判断其值。在上一个例子中,通过空合运算符为一个名为`colorNameToUse`的变量赋予一个字符串类型初始值。
由于`userDefinedColorName`值为空,因此表达式` userDefinedColorName ?? defaultColorName `返回默认值,即`red`
另一种情况,分配一个非空值(`non-nil`)给 `userDefinedColorName`,再次执行空合运算,运算结果为封包在`userDefaultColorName`中的值,而非默认值。
userDefinedColor = "green"
colorNameToUse = userDefinedColorName ?? defaultColorName
//userDefinedColor非空因此colorNameToUsede的值为绿色
<a name="nil_coalescing_operator"></a>
## 空合运算符(Nil Coalescing Operator)
空合运算符(`a ?? b`)将对可选类型`a`进行空判断,如果`a`包含一个值就进行解封,否则就返回一个默认值`b`.这个运算符有两个条件:
- 表达式`a`必须是Optional类型
- 默认值`b`的类型必须要和`a`存储值的类型保持一致
空合并运算符是对以下代码的简短表达方法
a != nil ? a! : b
上述代码使用了三目运算符。当可选类型`a`的值不为空时,进行强制解封(`a!`)访问`a`中值,反之当`a`中值为空时返回默认值b。无疑空合运算符(`??`)提供了一种更为优雅的方式去封装条件判断和解封两种行为,显得简洁以及更具可读性。
> 注意:
如果`a`为非空值(`non-nil`),那么值`b`将不会被估值。这也就是所谓的短路求值。
下文例子采用空合并运算符,实现了在默认颜色名和可选自定义颜色名之间抉择:
let defaultColorName = "red"
var userDefinedColorName:String? //默认值为nil
var colorNameToUse = userDefinedColorName ?? defaultColorName
//userDefinedColorName的值为空 所以colorNameToUse的值为`red`
`userDefinedColorName`变量被定义为一个可选字符串类型默认值为nil。由于`userDefinedColorName`是一个可选类型,我们可以使用空合运算符去判断其值。在上一个例子中,通过空合运算符为一个名为`colorNameToUse`的变量赋予一个字符串类型初始值。
由于`userDefinedColorName`值为空,因此表达式` userDefinedColorName ?? defaultColorName `返回默认值,即`red`
另一种情况,分配一个非空值(`non-nil`)给 `userDefinedColorName`,再次执行空合运算,运算结果为封包在`userDefaultColorName`中的值,而非默认值。
userDefinedColor = "green"
colorNameToUse = userDefinedColorName ?? defaultColorName
//userDefinedColor非空因此colorNameToUsede的值为绿色
<a name="range_operators"></a>
## 区间运算符

View File

@ -1,6 +1,10 @@
> 翻译:[wh1100717](https://github.com/wh1100717)
> 校对:[Hawstein](https://github.com/Hawstein)
<link rel="stylesheet" href="../assets/css/xcode.css">
<script src="../assets/js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
# 字符串和字符Strings and Characters
-----------------

View File

@ -1,6 +1,10 @@
> 翻译:[zqp](https://github.com/zqp)
> 校对:[shinyzhu](https://github.com/shinyzhu), [stanzhai](https://github.com/stanzhai)
<link rel="stylesheet" href="../assets/css/xcode.css">
<script src="../assets/js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
# 集合类型 (Collection Types)
-----------------
@ -89,9 +93,9 @@ shoppingList.append("Flour")
除此之外,使用加法赋值运算符(`+=`)也可以直接在数组后面添加一个或多个拥有相同类型的数据项:
```swift
shoppingList += ["Baking Powder"]
// shoppingList 现在有四项了
shoppingList += ["Chocolate Spread","Cheese","Butter"]
shoppingList += ["Baking Powder"]
// shoppingList 现在有四项了
shoppingList += ["Chocolate Spread","Cheese","Butter"]
// shoppingList 现在有七项了
```

View File

@ -1,6 +1,10 @@
> 翻译:[vclwei](https://github.com/vclwei), [coverxit](https://github.com/coverxit), [NicePiao](https://github.com/NicePiao)
> 校对:[coverxit](https://github.com/coverxit), [stanzhai](https://github.com/stanzhai)
<link rel="stylesheet" href="../assets/css/xcode.css">
<script src="../assets/js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
# 控制流
-----------------

View File

@ -1,6 +1,10 @@
> 翻译:[honghaoz](https://github.com/honghaoz)
> 校对:[LunaticM](https://github.com/LunaticM)
<link rel="stylesheet" href="../assets/css/xcode.css">
<script src="../assets/js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
# 函数Functions
-----------------

View File

@ -1,6 +1,10 @@
> 翻译:[wh1100717](https://github.com/wh1100717)
> 校对:[lyuka](https://github.com/lyuka)
<link rel="stylesheet" href="../assets/css/xcode.css">
<script src="../assets/js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
# 闭包Closures
-----------------

View File

@ -1,6 +1,10 @@
> 翻译:[yankuangshi](https://github.com/yankuangshi)
> 校对:[shinyzhu](https://github.com/shinyzhu)
<link rel="stylesheet" href="../assets/css/xcode.css">
<script src="../assets/js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
# 枚举Enumerations
---

View File

@ -1,6 +1,10 @@
> 翻译:[JaySurplus](https://github.com/JaySurplus)
> 校对:[sg552](https://github.com/sg552)
<link rel="stylesheet" href="../assets/css/xcode.css">
<script src="../assets/js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
# 类和结构体
本页包含内容:

View File

@ -1,6 +1,10 @@
> 翻译:[shinyzhu](https://github.com/shinyzhu)
> 校对:[pp-prog](https://github.com/pp-prog)
<link rel="stylesheet" href="../assets/css/xcode.css">
<script src="../assets/js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
# 属性 (Properties)
---

View File

@ -1,6 +1,10 @@
> 翻译:[pp-prog](https://github.com/pp-prog)
> 校对:[zqp](https://github.com/zqp)
<link rel="stylesheet" href="../assets/css/xcode.css">
<script src="../assets/js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
# 方法Methods
-----------------

View File

@ -1,6 +1,10 @@
> 翻译:[siemenliu](https://github.com/siemenliu)
> 校对:[zq54zquan](https://github.com/zq54zquan)
<link rel="stylesheet" href="../assets/css/xcode.css">
<script src="../assets/js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
# 下标脚本Subscripts
-----------------

View File

@ -1,6 +1,10 @@
> 翻译:[Hawstein](https://github.com/Hawstein)
> 校对:[menlongsheng](https://github.com/menlongsheng)
<link rel="stylesheet" href="../assets/css/xcode.css">
<script src="../assets/js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
# 继承Inheritance
-------------------

View File

@ -1,6 +1,10 @@
> 翻译:[lifedim](https://github.com/lifedim)
> 校对:[lifedim](https://github.com/lifedim)
<link rel="stylesheet" href="../assets/css/xcode.css">
<script src="../assets/js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
# 构造过程Initialization
-----------------

View File

@ -1,6 +1,10 @@
> 翻译:[bruce0505](https://github.com/bruce0505)
> 校对:[fd5788](https://github.com/fd5788)
<link rel="stylesheet" href="../assets/css/xcode.css">
<script src="../assets/js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
# 析构过程Deinitialization
---------------------------

View File

@ -1,6 +1,10 @@
> 翻译:[TimothyYe](https://github.com/TimothyYe)
> 校对:[Hawstein](https://github.com/Hawstein)
<link rel="stylesheet" href="../assets/css/xcode.css">
<script src="../assets/js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
# 自动引用计数
-----------------

View File

@ -1,6 +1,10 @@
> 翻译:[Jasonbroker](https://github.com/Jasonbroker)
> 校对:[numbbbbb](https://github.com/numbbbbb), [stanzhai](https://github.com/stanzhai)
<link rel="stylesheet" href="../assets/css/xcode.css">
<script src="../assets/js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
# Optional Chaining
-----------------

View File

@ -1,6 +1,10 @@
> 翻译:[xiehurricane](https://github.com/xiehurricane)
> 校对:[happyming](https://github.com/happyming)
<link rel="stylesheet" href="../assets/css/xcode.css">
<script src="../assets/js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
# 类型转换Type Casting
-----------------

View File

@ -1,6 +1,10 @@
> 翻译:[Lin-H](https://github.com/Lin-H)
> 校对:[shinyzhu](https://github.com/shinyzhu)
<link rel="stylesheet" href="../assets/css/xcode.css">
<script src="../assets/js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
# 嵌套类型
-----------------

View File

@ -1,6 +1,10 @@
> 翻译:[lyuka](https://github.com/lyuka)
> 校对:[Hawstein](https://github.com/Hawstein)
<link rel="stylesheet" href="../assets/css/xcode.css">
<script src="../assets/js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
#扩展Extensions
----

View File

@ -1,6 +1,10 @@
> 翻译:[geek5nan](https://github.com/geek5nan)
> 校对:[dabing1022](https://github.com/dabing1022)
<link rel="stylesheet" href="../assets/css/xcode.css">
<script src="../assets/js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
# 协议
-----------------

View File

@ -2,6 +2,10 @@
> 翻译:[takalard](https://github.com/takalard)
> 校对:[lifedim](https://github.com/lifedim)
<link rel="stylesheet" href="../assets/css/xcode.css">
<script src="../assets/js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
# 泛型
------

View File

@ -1,6 +1,10 @@
> 翻译:[JaceFu](http://www.devtalking.com/)
> 校对:[ChildhoodAndy](http://childhood.logdown.com)
<link rel="stylesheet" href="../assets/css/xcode.css">
<script src="../assets/js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
# 访问控制
------------------

View File

@ -1,6 +1,10 @@
> 翻译:[xielingwang](https://github.com/xielingwang)
> 校对:[numbbbbb](https://github.com/numbbbbb)
<link rel="stylesheet" href="../assets/css/xcode.css">
<script src="../assets/js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
# 高级运算符
-----------------

View File

@ -1,6 +1,10 @@
> 翻译:[dabing1022](https://github.com/dabing1022)
> 校对:[numbbbbb](https://github.com/numbbbbb)
<link rel="stylesheet" href="../assets/css/xcode.css">
<script src="../assets/js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
# 关于语言附注
-----------------

View File

@ -1,6 +1,10 @@
> 翻译:[superkam](https://github.com/superkam)
> 校对:[numbbbbb](https://github.com/numbbbbb)
<link rel="stylesheet" href="../assets/css/xcode.css">
<script src="../assets/js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
# 词法结构
-----------------

View File

@ -1,6 +1,10 @@
> 翻译:[lyuka](https://github.com/lyuka)
> 校对:[numbbbbb](https://github.com/numbbbbb), [stanzhai](https://github.com/stanzhai)
<link rel="stylesheet" href="../assets/css/xcode.css">
<script src="../assets/js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
# 类型Types
-----------------

View File

@ -1,6 +1,10 @@
> 翻译:[sg552](https://github.com/sg552)
> 校对:[numbbbbb](https://github.com/numbbbbb), [stanzhai](https://github.com/stanzhai)
<link rel="stylesheet" href="../assets/css/xcode.css">
<script src="../assets/js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
# 表达式Expressions
-----------------

View File

@ -1,6 +1,10 @@
> 翻译:[marsprince](https://github.com/marsprince)
> 校对:[numbbbbb](https://github.com/numbbbbb), [stanzhai](https://github.com/stanzhai)
<link rel="stylesheet" href="../assets/css/xcode.css">
<script src="../assets/js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
# 声明
-----------------

View File

@ -1,6 +1,10 @@
> 翻译:[Hawstein](https://github.com/Hawstein)
> 校对:[numbbbbb](https://github.com/numbbbbb), [stanzhai](https://github.com/stanzhai)
<link rel="stylesheet" href="../assets/css/xcode.css">
<script src="../assets/js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
# 特性
-----------------

View File

@ -1,6 +1,10 @@
> 翻译:[honghaoz](https://github.com/honghaoz)
> 校对:[numbbbbb](https://github.com/numbbbbb), [stanzhai](https://github.com/stanzhai)
<link rel="stylesheet" href="../assets/css/xcode.css">
<script src="../assets/js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
# 模式Patterns
-----------------

View File

@ -1,6 +1,10 @@
> 翻译:[fd5788](https://github.com/fd5788)
> 校对:[yankuangshi](https://github.com/yankuangshi), [stanzhai](https://github.com/stanzhai)
<link rel="stylesheet" href="../assets/css/xcode.css">
<script src="../assets/js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
# 泛型参数
---------

View File

@ -1,6 +1,10 @@
> 翻译:[stanzhai](https://github.com/stanzhai)
> 校对:[xielingwang](https://github.com/xielingwang)
<link rel="stylesheet" href="../assets/css/xcode.css">
<script src="../assets/js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
# 语法总结
_________________

View File

@ -1,6 +1,10 @@
> 翻译:[coverxit](https://github.com/coverxit)
> 校对:[numbbbbb](https://github.com/numbbbbb), [coverxit](https://github.com/coverxit), [stanzhai](https://github.com/stanzhai)
<link rel="stylesheet" href="../assets/css/xcode.css">
<script src="../assets/js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
# 语句
-----------------

View File

@ -1,6 +1,10 @@
> 翻译:[老码团队翻译组-Arya](http://weibo.com/littlekok/)
> 校对:[老码团队翻译组-Oberyn](http://weibo.com/u/5241713117)
<link rel="stylesheet" href="../assets/css/xcode.css">
<script src="../assets/js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
# Access Control 权限控制的黑与白
如果您之前没有接触过权限控制,先来听一个小故事:

View File

@ -1,6 +1,10 @@
> 翻译:[老码团队翻译组-Tyrion](http://weibo.com/u/5241713117)
> 校对:[老码团队翻译组-Oberyn](http://weibo.com/u/5241713117)
<link rel="stylesheet" href="../assets/css/xcode.css">
<script src="../assets/js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
# 造个类型不是梦-白话Swift类型创建
-----------------

View File

@ -1,6 +1,10 @@
> 翻译:[老码团队翻译组-Arya](http://weibo.com/littlekok/)
> 校对:[老码团队翻译组-Jame](http://weibo.com/u/5241713117)
<link rel="stylesheet" href="../assets/css/xcode.css">
<script src="../assets/js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
# WWDC里面的那个“大炮打气球”
![Ballon playground](https://devimages.apple.com.edgekey.net/swift/images/swift-screenshot.jpg)

View File

@ -1,6 +1,10 @@
> 翻译:[老码团队翻译组-Relly](http://weibo.com/penguinliong/)
> 校对:[老码团队翻译组-Tyrion](http://weibo.com/u/5241713117)
<link rel="stylesheet" href="../assets/css/xcode.css">
<script src="../assets/js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
# Swift与C语言指针友好合作
-----------------

View File

@ -1,6 +1,10 @@
> 翻译:[老码团队翻译组-Arya](http://weibo.com/littlekok/)
> 校对:[老码团队翻译组-Jame](http://weibo.com/u/5241713117)
<link rel="stylesheet" href="../assets/css/xcode.css">
<script src="../assets/js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
# Swift里的值类型与引用类型
-----------------

View File

@ -1,6 +1,10 @@
> 翻译:[老码团队翻译组-Arya](http://weibo.com/littlekok/)
> 校对:[老码团队翻译组-Jame](http://weibo.com/u/5241713117)
<link rel="stylesheet" href="../assets/css/xcode.css">
<script src="../assets/js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
# 访问控制和protected
-----------------