make gitbook
This commit is contained in:
@ -16,6 +16,8 @@
|
||||
|
||||
|
||||
|
||||
<meta name="author" content="https:">
|
||||
|
||||
|
||||
<link rel="next" href="../chapter2/03_Strings_and_Characters.html" />
|
||||
|
||||
@ -32,12 +34,12 @@
|
||||
|
||||
|
||||
|
||||
<div class="book" data-level="2.2" data-basepath=".." data-revision="1402903834498">
|
||||
<div class="book" data-github="https://github.com/numbbbbb/the-swift-programming-language-in-chinese" data-level="2.2" data-basepath=".." data-revision="1402929590291">
|
||||
<div class="book-header">
|
||||
<!-- Actions Left -->
|
||||
<a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>
|
||||
|
||||
<a href="https://github.com/null" target="_blank" class="btn pull-left home-bookmark" aria-label="GitHub home"><i class="fa fa-bookmark-o"></i></a>
|
||||
<a href="https://github.com/https://github.com/numbbbbb/the-swift-programming-language-in-chinese" target="_blank" class="btn pull-left home-bookmark" aria-label="GitHub home"><i class="fa fa-bookmark-o"></i></a>
|
||||
|
||||
<a href="#" class="btn pull-left toggle-search" aria-label="Toggle search"><i class="fa fa-search"></i></a>
|
||||
<span id="font-settings-wrapper">
|
||||
@ -80,6 +82,9 @@
|
||||
|
||||
|
||||
|
||||
<a href="https://github.com/https://github.com/numbbbbb/the-swift-programming-language-in-chinese/stargazers" target="_blank" class="btn pull-right count-star hidden-xs"><i class="fa fa-star-o"></i> Star (<span>-</span>)</a>
|
||||
<a href="https://github.com/https://github.com/numbbbbb/the-swift-programming-language-in-chinese/watchers" target="_blank" class="btn pull-right count-watch hidden-xs"><i class="fa fa-eye"></i> Watch (<span>-</span>)</a>
|
||||
|
||||
|
||||
<!-- Title -->
|
||||
<h1>
|
||||
@ -97,12 +102,29 @@
|
||||
<ul class="summary">
|
||||
|
||||
|
||||
|
||||
|
||||
<li>
|
||||
<a href="https://github.com/https:" target="blank" class="author-link">About the author</a>
|
||||
</li>
|
||||
|
||||
|
||||
|
||||
|
||||
<li>
|
||||
<a href="https://github.com/https://github.com/numbbbbb/the-swift-programming-language-in-chinese/issues" target="blank"class="issues-link">Questions and Issues</a>
|
||||
</li>
|
||||
|
||||
|
||||
|
||||
|
||||
<li>
|
||||
<a href="https://github.com/https://github.com/numbbbbb/the-swift-programming-language-in-chinese/edit/master/chapter2/02_Basic_Operators.md" target="blank" class="contribute-link">Edit and Contribute</a>
|
||||
</li>
|
||||
|
||||
|
||||
|
||||
<li class="divider"></li>
|
||||
|
||||
|
||||
<li data-level="0" data-path="index.html">
|
||||
<a href="../"><i class="fa fa-check"></i> 序</a>
|
||||
@ -573,7 +595,7 @@
|
||||
|
||||
<div class="page-inner">
|
||||
|
||||
<section class="normal" id="section-gitbook_113">
|
||||
<section class="normal" id="section-gitbook_32">
|
||||
|
||||
<blockquote>
|
||||
<p>翻译:<a href="https://github.com/xielingwang" target="_blank">xielingwang</a><br>校对:<a href="https://github.com/Evilcome" target="_blank">Evilcome</a></p>
|
||||
@ -591,13 +613,13 @@
|
||||
<li><a href="#range_operators">区间运算符</a></li>
|
||||
<li><a href="#logical_operators">逻辑运算符</a></li>
|
||||
</ul>
|
||||
<p>运算符是检查,改变,合并值的特殊符号或短语。例如,加号<code>+</code>将两个数相加(如<code>let i = 1 + 2</code>)。复杂些的运行算例如逻辑与运算符<code>&&</code>(如<code>if enteredDoorCode && passedRetinaScan</code>),又或让 i 值加1的便捷运算符自增运算符<code>++i</code>等。</p>
|
||||
<p>Swift 支持大部分标准 C 语言的运算符,且改进许多特性来减少常规编码错误。如,赋值符(<code>=</code>)不返回值,以防止把想要判断相等运算符(<code>==</code>)的地方写成赋值符导致的错误。数值运算符(<code>+</code>,<code>-</code>,<code>*</code>,<code>/</code>,<code>%</code>等)会检测并不允许值溢出,以此来避免保存变量时由于变量大于或小于其类型所能承载的范围时导致的异常结果。当然允许你使用 Swift 的溢出运算符来实现溢出。详情参见<a href="23_Advanced_Operators.html#overflow_operators">溢出运算符</a>。</p>
|
||||
<p>运算符是检查、改变、合并值的特殊符号或短语。例如,加号<code>+</code>将两个数相加(如<code>let i = 1 + 2</code>)。复杂些的运算例如逻辑与运算符<code>&&</code>(如<code>if enteredDoorCode && passedRetinaScan</code>),或让 i 值加1的便捷自增运算符<code>++i</code>等。</p>
|
||||
<p>Swift 支持大部分标准 C 语言的运算符,且改进许多特性来减少常规编码错误。如:赋值符(<code>=</code>)不返回值,以防止把想要判断相等运算符(<code>==</code>)的地方写成赋值符导致的错误。数值运算符(<code>+</code>,<code>-</code>,<code>*</code>,<code>/</code>,<code>%</code>等)会检测并不允许值溢出,以此来避免保存变量时由于变量大于或小于其类型所能承载的范围时导致的异常结果。当然允许你使用 Swift 的溢出运算符来实现溢出。详情参见<a href="23_Advanced_Operators.html#overflow_operators">溢出运算符</a>。</p>
|
||||
<p>区别于 C 语言,在 Swift 中你可以对浮点数进行取余运算(<code>%</code>),Swift 还提供了 C 语言没有的表达两数之间的值的区间运算符,(<code>a..b</code>和<code>a...b</code>),这方便我们表达一个区间内的数值。</p>
|
||||
<p>本章节只描述了 Swift 中的基本运算符,<a href="23_Advanced_Operators.html">高级运算符</a>包含了高级运算符,及如何自定义运算符,及如何进行自定义类型的运算符重载。</p>
|
||||
<p><a name="terminology"></a></p>
|
||||
<h2 id="-">术语</h2>
|
||||
<p>运算符有一元,二元和三元运算符。</p>
|
||||
<p>运算符有一元、二元和三元运算符。</p>
|
||||
<ul>
|
||||
<li>一元运算符对单一操作对象操作(如<code>-a</code>)。一元运算符分前置符和后置运算符,前置运算符需紧排操作对象之前(如<code>!b</code>),后置运算符需紧跟操作对象之后(如<code>i++</code>)。</li>
|
||||
<li>二元运算符操作两个操作对象(如<code>2 + 3</code>),是中置的,因为它们出现在两个操作对象之间。</li>
|
||||
@ -621,10 +643,10 @@ a = b
|
||||
// 此句错误, 因为 x = y 并不返回任何值
|
||||
}
|
||||
</code></pre>
|
||||
<p>这个特性使得你无法把(<code>==</code>)错写成(<code>=</code>)了,由于<code>if x = y</code>是错误代码,Swift 从底层帮你避免了这些代码错误。</p>
|
||||
<p>这个特性使你无法把(<code>==</code>)错写成(<code>=</code>),由于<code>if x = y</code>是错误代码,Swift 从底层帮你避免了这些错误代码。</p>
|
||||
<p><a name="arithmetic_operators"></a></p>
|
||||
<h2 id="-">数值运算</h2>
|
||||
<p>Swift 让所有数值类型都支持了基本的四则运算:</p>
|
||||
<p>Swift 中所有数值类型都支持了基本的四则运算:</p>
|
||||
<ul>
|
||||
<li>加法(<code>+</code>)</li>
|
||||
<li>减法(<code>-</code>)</li>
|
||||
@ -637,7 +659,7 @@ a = b
|
||||
10.0 / 2.5 // 等于 4.0
|
||||
</code></pre>
|
||||
<p>与 C 语言和 Objective-C 不同的是,Swift 默认不允许在数值运算中出现溢出情况。但你可以使用 Swift 的溢出运算符来达到你有目的的溢出(如<code>a &+ b</code>)。详情参见<a href="23_Advanced_Operators.html#overflow_operators">溢出运算符</a>。</p>
|
||||
<p>加法运算符也用于<code>String</code>的拼接:</p>
|
||||
<p>加法运算符也可用于<code>String</code>的拼接:</p>
|
||||
<pre><code class="lang-swift">"hello, " + "world" // 等于 "hello, world"
|
||||
</code></pre>
|
||||
<p>两个<code>Character</code>值或一个<code>String</code>和一个<code>Character</code>值,相加会生成一个新的<code>String</code>值:</p>
|
||||
|
||||
Reference in New Issue
Block a user