Files
the-swift-programming-langu…/chapter2/02_Basic_Operators.html
2014-06-11 21:26:49 +08:00

951 lines
44 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE HTML>
<html lang="en-US" manifest="../manifest.appcache">
<head prefix="og: http://ogp.me/ns# book: http://ogp.me/ns/book#">
<meta charset="UTF-8">
<title>基本运算符 | Swift 编程语言</title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<meta name="robots" content="index, follow">
<meta name="author" content="">
<meta name="description" content="Swift 是苹果在 WWDC 2014 上发布的一款全新的编程语言,本书译自苹果官方的 Swift 教程《The Swift Programming Language》。">
<meta name="keywords" content="gitbook,github" >
<meta name="generator" content="www.gitbook.io">
<link rel="next" href="../chapter2/03_Strings_and_Characters.html" />
<link rel="prev" href="../chapter2/01_The_Basics.html" />
<meta property="og:title" content="基本运算符 | Swift 编程语言">
<meta property="og:site_name" content="Swift 编程语言">
<meta property="og:type" content="book">
<meta property="og:locale" content="en_US">
<meta property="book:author" content="https://github.com/">
<meta property="book:tag" content="GitBook">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
</head>
<body>
<link rel="stylesheet" href="../gitbook/style.css">
<div class="book" data-level="2.2" data-basepath=".." data-revision="1402493167130">
<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="#" class="btn pull-left toggle-search" aria-label="Toggle search"><i class="fa fa-search"></i></a>
<span id="font-settings-wrapper">
<a href="#" class="btn pull-left toggle-font-settings" aria-label="Toggle font settings"><i class="fa fa-font"></i>
</a>
<div class="dropdown-menu font-settings">
<div class="dropdown-caret">
<span class="caret-outer"></span>
<span class="caret-inner"></span>
</div>
<div class="btn-group btn-block">
<button id="reduce-font-size" class="btn btn-default">A</button>
<button id="enlarge-font-size" class="btn btn-default">A</button>
</div>
<ul class="list-group font-family-list">
<li class="list-group-item" data-font="0">Serif</li>
<li class="list-group-item" data-font="1">Sans</li>
</ul>
<div class="btn-group btn-group-xs btn-block color-theme-list">
<button type="button" class="btn btn-default" id="color-theme-preview-0" data-theme="0">White</button>
<button type="button" class="btn btn-default" id="color-theme-preview-1" data-theme="1">Sepia</button>
<button type="button" class="btn btn-default" id="color-theme-preview-2" data-theme="2">Night</button>
</div>
</div>
</span>
<!-- Actions Right -->
<a href="#" target="_blank" class="btn pull-right google-plus-sharing-link sharing-link" data-sharing="google-plus" aria-label="Share on Google Plus"><i class="fa fa-google-plus"></i></a>
<a href="#" target="_blank" class="btn pull-right facebook-sharing-link sharing-link" data-sharing="facebook" aria-label="Share on Facebook"><i class="fa fa-facebook"></i></a>
<a href="#" target="_blank" class="btn pull-right twitter-sharing-link sharing-link" data-sharing="twitter" aria-label="Share on Twitter"><i class="fa fa-twitter"></i></a>
<!-- Title -->
<h1>
<i class="fa fa-spinner fa-spin"></i>
<a href="../" >Swift 编程语言</a>
</h1>
</div>
<div class="book-summary">
<div class="book-search">
<input type="text" placeholder="Search" class="form-control" />
</div>
<ul class="summary">
<li data-level="0" data-path="index.html">
<a href="../"><i class="fa fa-check"></i> Introduction</a>
</li>
<li class="chapter " data-level="1" data-path="chapter1/chapter1.html">
<a href="../chapter1/chapter1.html">
<i class="fa fa-check"></i> <b>1.</b> 欢迎使用 Swift
</a>
<ul class="articles">
<li class="chapter " data-level="1.1" data-path="chapter1/01_swift.html">
<a href="../chapter1/01_swift.html">
<i class="fa fa-check"></i> <b>1.1.</b> 关于 Swift
</a>
</li>
<li class="chapter " data-level="1.2" data-path="chapter1/02_a_swift_tour.html">
<a href="../chapter1/02_a_swift_tour.html">
<i class="fa fa-check"></i> <b>1.2.</b> Swift 初见
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="2" data-path="chapter2/chapter2.html">
<a href="../chapter2/chapter2.html">
<i class="fa fa-check"></i> <b>2.</b> Swift 教程
</a>
<ul class="articles">
<li class="chapter " data-level="2.1" data-path="chapter2/01_The_Basics.html">
<a href="../chapter2/01_The_Basics.html">
<i class="fa fa-check"></i> <b>2.1.</b> 基础部分
</a>
</li>
<li class="chapter " data-level="2.2" data-path="chapter2/02_Basic_Operators.html">
<a href="../chapter2/02_Basic_Operators.html">
<i class="fa fa-check"></i> <b>2.2.</b> 基本运算符
</a>
</li>
<li class="chapter " data-level="2.3" data-path="chapter2/03_Strings_and_Characters.html">
<a href="../chapter2/03_Strings_and_Characters.html">
<i class="fa fa-check"></i> <b>2.3.</b> 字符串和字符
</a>
</li>
<li class="chapter " data-level="2.4" data-path="chapter2/04_Collection_Types.html">
<a href="../chapter2/04_Collection_Types.html">
<i class="fa fa-check"></i> <b>2.4.</b> 集合类型
</a>
</li>
<li class="chapter " data-level="2.5" data-path="chapter2/05_Control_Flow.html">
<a href="../chapter2/05_Control_Flow.html">
<i class="fa fa-check"></i> <b>2.5.</b> 控制流
</a>
</li>
<li class="chapter " data-level="2.6" data-path="chapter2/06_Functions.html">
<a href="../chapter2/06_Functions.html">
<i class="fa fa-check"></i> <b>2.6.</b> 函数
</a>
</li>
<li class="chapter " data-level="2.7" data-path="chapter2/07_Closures.html">
<a href="../chapter2/07_Closures.html">
<i class="fa fa-check"></i> <b>2.7.</b> 闭包
</a>
</li>
<li class="chapter " data-level="2.8" data-path="chapter2/08_Enumerations.html">
<a href="../chapter2/08_Enumerations.html">
<i class="fa fa-check"></i> <b>2.8.</b> 枚举
</a>
</li>
<li class="chapter " data-level="2.9" data-path="chapter2/09_Classes_and_Structures.html">
<a href="../chapter2/09_Classes_and_Structures.html">
<i class="fa fa-check"></i> <b>2.9.</b> 类和结构体
</a>
</li>
<li class="chapter " data-level="2.10" data-path="chapter2/10_Properties.html">
<a href="../chapter2/10_Properties.html">
<i class="fa fa-check"></i> <b>2.10.</b> 属性
</a>
</li>
<li class="chapter " data-level="2.11" data-path="chapter2/11_Methods.html">
<a href="../chapter2/11_Methods.html">
<i class="fa fa-check"></i> <b>2.11.</b> 方法
</a>
</li>
<li class="chapter " data-level="2.12" data-path="chapter2/12_Subscripts.html">
<a href="../chapter2/12_Subscripts.html">
<i class="fa fa-check"></i> <b>2.12.</b> 附属脚本
</a>
</li>
<li class="chapter " data-level="2.13" data-path="chapter2/13_Inheritance.html">
<a href="../chapter2/13_Inheritance.html">
<i class="fa fa-check"></i> <b>2.13.</b> 继承
</a>
</li>
<li class="chapter " data-level="2.14" data-path="chapter2/14_Initialization.html">
<a href="../chapter2/14_Initialization.html">
<i class="fa fa-check"></i> <b>2.14.</b> 构造过程
</a>
</li>
<li class="chapter " data-level="2.15" data-path="chapter2/15_Deinitialization.html">
<a href="../chapter2/15_Deinitialization.html">
<i class="fa fa-check"></i> <b>2.15.</b> 析构过程
</a>
</li>
<li class="chapter " data-level="2.16" data-path="chapter2/16_Automatic_Reference_Counting.html">
<a href="../chapter2/16_Automatic_Reference_Counting.html">
<i class="fa fa-check"></i> <b>2.16.</b> 自动引用计数
</a>
</li>
<li class="chapter " data-level="2.17" data-path="chapter2/17_Optional_Chaining.html">
<a href="../chapter2/17_Optional_Chaining.html">
<i class="fa fa-check"></i> <b>2.17.</b> 可选链
</a>
</li>
<li class="chapter " data-level="2.18" data-path="chapter2/18_Type_Casting.html">
<a href="../chapter2/18_Type_Casting.html">
<i class="fa fa-check"></i> <b>2.18.</b> 类型检查
</a>
</li>
<li class="chapter " data-level="2.19" data-path="chapter2/19_Nested_Types.html">
<a href="../chapter2/19_Nested_Types.html">
<i class="fa fa-check"></i> <b>2.19.</b> 嵌套类型
</a>
</li>
<li class="chapter " data-level="2.20" data-path="chapter2/20_Extensions.html">
<a href="../chapter2/20_Extensions.html">
<i class="fa fa-check"></i> <b>2.20.</b> 扩展
</a>
</li>
<li class="chapter " data-level="2.21" data-path="chapter2/21_Protocols.html">
<a href="../chapter2/21_Protocols.html">
<i class="fa fa-check"></i> <b>2.21.</b> 协议
</a>
</li>
<li class="chapter " data-level="2.22" data-path="chapter2/22_Generics.html">
<a href="../chapter2/22_Generics.html">
<i class="fa fa-check"></i> <b>2.22.</b> 泛型
</a>
</li>
<li class="chapter " data-level="2.23" data-path="chapter2/23_Advanced_Operators.html">
<a href="../chapter2/23_Advanced_Operators.html">
<i class="fa fa-check"></i> <b>2.23.</b> 高级操作符
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="3" data-path="chapter3/chapter3.html">
<a href="../chapter3/chapter3.html">
<i class="fa fa-check"></i> <b>3.</b> 语言参考
</a>
<ul class="articles">
<li class="chapter " data-level="3.1" data-path="chapter3/01_About_the_Language_Reference.html">
<a href="../chapter3/01_About_the_Language_Reference.html">
<i class="fa fa-check"></i> <b>3.1.</b> 关于语言参考
</a>
</li>
<li class="chapter " data-level="3.2" data-path="chapter3/02_Lexical_Structure.html">
<a href="../chapter3/02_Lexical_Structure.html">
<i class="fa fa-check"></i> <b>3.2.</b> 词法结构
</a>
</li>
<li class="chapter " data-level="3.3" data-path="chapter3/03_Types.html">
<a href="../chapter3/03_Types.html">
<i class="fa fa-check"></i> <b>3.3.</b> 类型
</a>
</li>
<li class="chapter " data-level="3.4" data-path="chapter3/04_Expressions.html">
<a href="../chapter3/04_Expressions.html">
<i class="fa fa-check"></i> <b>3.4.</b> 表达式
</a>
</li>
<li class="chapter " data-level="3.5" data-path="chapter3/10_Statements.html">
<a href="../chapter3/10_Statements.html">
<i class="fa fa-check"></i> <b>3.5.</b> 语句
</a>
</li>
<li class="chapter " data-level="3.6" data-path="chapter3/05_Declarations.html">
<a href="../chapter3/05_Declarations.html">
<i class="fa fa-check"></i> <b>3.6.</b> 声明
</a>
</li>
<li class="chapter " data-level="3.7" data-path="chapter3/06_Attributes.html">
<a href="../chapter3/06_Attributes.html">
<i class="fa fa-check"></i> <b>3.7.</b> 特性
</a>
</li>
<li class="chapter " data-level="3.8" data-path="chapter3/07_Patterns.html">
<a href="../chapter3/07_Patterns.html">
<i class="fa fa-check"></i> <b>3.8.</b> 模式
</a>
</li>
<li class="chapter " data-level="3.9" data-path="chapter3/08_Generic_Parameters_and_Arguments.html">
<a href="../chapter3/08_Generic_Parameters_and_Arguments.html">
<i class="fa fa-check"></i> <b>3.9.</b> 泛型参数
</a>
</li>
<li class="chapter " data-level="3.10" data-path="chapter3/09_Summary_of_the_Grammar.html">
<a href="../chapter3/09_Summary_of_the_Grammar.html">
<i class="fa fa-check"></i> <b>3.10.</b> 语法总结
</a>
</li>
</ul>
</li>
<li class="divider"></li>
<li>
<a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Generated using GitBook</a>
</li>
<li style="margin-left:15%;"> <iframe src="http://ghbtns.com/github-btn.html?user=numbbbbb&repo=the-swift-programming-language-in-chinese&type=watch&count=true&size=large"
allowtransparency="true" frameborder="0" scrolling="0" width="170" height="30"></iframe></li>
</ul>
</div>
<div class="book-body">
<div class="body-inner">
<div class="page-wrapper" tabindex="-1">
<div class="book-progress">
<div class="bar">
<div class="inner" style="width: 42.10526315789474%;min-width: 39.473684210526315%;"></div>
</div>
<div class="chapters">
<a href="../index.html" title="Introduction" class="chapter done new-chapter" data-progress="0" style="left: 0%;"></a>
<a href="../chapter1/chapter1.html" title="欢迎使用 Swift" class="chapter done new-chapter" data-progress="1" style="left: 2.6315789473684212%;"></a>
<a href="../chapter1/01_swift.html" title="关于 Swift" class="chapter done " data-progress="1.1" style="left: 5.2631578947368425%;"></a>
<a href="../chapter1/02_a_swift_tour.html" title="Swift 初见" class="chapter done " data-progress="1.2" style="left: 7.894736842105263%;"></a>
<a href="../chapter2/chapter2.html" title="Swift 教程" class="chapter done new-chapter" data-progress="2" style="left: 10.526315789473685%;"></a>
<a href="../chapter2/01_The_Basics.html" title="基础部分" class="chapter done " data-progress="2.1" style="left: 13.157894736842104%;"></a>
<a href="../chapter2/10_Properties.html" title="属性" class="chapter done " data-progress="2.10" style="left: 15.789473684210526%;"></a>
<a href="../chapter2/11_Methods.html" title="方法" class="chapter done " data-progress="2.11" style="left: 18.42105263157895%;"></a>
<a href="../chapter2/12_Subscripts.html" title="附属脚本" class="chapter done " data-progress="2.12" style="left: 21.05263157894737%;"></a>
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter done " data-progress="2.13" style="left: 23.68421052631579%;"></a>
<a href="../chapter2/14_Initialization.html" title="构造过程" class="chapter done " data-progress="2.14" style="left: 26.31578947368421%;"></a>
<a href="../chapter2/15_Deinitialization.html" title="析构过程" class="chapter done " data-progress="2.15" style="left: 28.94736842105263%;"></a>
<a href="../chapter2/16_Automatic_Reference_Counting.html" title="自动引用计数" class="chapter done " data-progress="2.16" style="left: 31.57894736842105%;"></a>
<a href="../chapter2/17_Optional_Chaining.html" title="可选链" class="chapter done " data-progress="2.17" style="left: 34.21052631578947%;"></a>
<a href="../chapter2/18_Type_Casting.html" title="类型检查" class="chapter done " data-progress="2.18" style="left: 36.8421052631579%;"></a>
<a href="../chapter2/19_Nested_Types.html" title="嵌套类型" class="chapter done " data-progress="2.19" style="left: 39.473684210526315%;"></a>
<a href="../chapter2/02_Basic_Operators.html" title="基本运算符" class="chapter done " data-progress="2.2" style="left: 42.10526315789474%;"></a>
<a href="../chapter2/20_Extensions.html" title="扩展" class="chapter " data-progress="2.20" style="left: 44.73684210526316%;"></a>
<a href="../chapter2/21_Protocols.html" title="协议" class="chapter " data-progress="2.21" style="left: 47.36842105263158%;"></a>
<a href="../chapter2/22_Generics.html" title="泛型" class="chapter " data-progress="2.22" style="left: 50%;"></a>
<a href="../chapter2/23_Advanced_Operators.html" title="高级操作符" class="chapter " data-progress="2.23" style="left: 52.63157894736842%;"></a>
<a href="../chapter2/03_Strings_and_Characters.html" title="字符串和字符" class="chapter " data-progress="2.3" style="left: 55.26315789473684%;"></a>
<a href="../chapter2/04_Collection_Types.html" title="集合类型" class="chapter " data-progress="2.4" style="left: 57.89473684210526%;"></a>
<a href="../chapter2/05_Control_Flow.html" title="控制流" class="chapter " data-progress="2.5" style="left: 60.526315789473685%;"></a>
<a href="../chapter2/06_Functions.html" title="函数" class="chapter " data-progress="2.6" style="left: 63.1578947368421%;"></a>
<a href="../chapter2/07_Closures.html" title="闭包" class="chapter " data-progress="2.7" style="left: 65.78947368421052%;"></a>
<a href="../chapter2/08_Enumerations.html" title="枚举" class="chapter " data-progress="2.8" style="left: 68.42105263157895%;"></a>
<a href="../chapter2/09_Classes_and_Structures.html" title="类和结构体" class="chapter " data-progress="2.9" style="left: 71.05263157894737%;"></a>
<a href="../chapter3/chapter3.html" title="语言参考" class="chapter new-chapter" data-progress="3" style="left: 73.6842105263158%;"></a>
<a href="../chapter3/01_About_the_Language_Reference.html" title="关于语言参考" class="chapter " data-progress="3.1" style="left: 76.3157894736842%;"></a>
<a href="../chapter3/09_Summary_of_the_Grammar.html" title="语法总结" class="chapter " data-progress="3.10" style="left: 78.94736842105263%;"></a>
<a href="../chapter3/02_Lexical_Structure.html" title="词法结构" class="chapter " data-progress="3.2" style="left: 81.57894736842105%;"></a>
<a href="../chapter3/03_Types.html" title="类型" class="chapter " data-progress="3.3" style="left: 84.21052631578948%;"></a>
<a href="../chapter3/04_Expressions.html" title="表达式" class="chapter " data-progress="3.4" style="left: 86.84210526315789%;"></a>
<a href="../chapter3/10_Statements.html" title="语句" class="chapter " data-progress="3.5" style="left: 89.47368421052632%;"></a>
<a href="../chapter3/05_Declarations.html" title="声明" class="chapter " data-progress="3.6" style="left: 92.10526315789474%;"></a>
<a href="../chapter3/06_Attributes.html" title="特性" class="chapter " data-progress="3.7" style="left: 94.73684210526316%;"></a>
<a href="../chapter3/07_Patterns.html" title="模式" class="chapter " data-progress="3.8" style="left: 97.36842105263158%;"></a>
<a href="../chapter3/08_Generic_Parameters_and_Arguments.html" title="泛型参数" class="chapter " data-progress="3.9" style="left: 100%;"></a>
</div>
</div>
<div class="page-inner">
<section class="normal" id="section-gitbook_144">
<h1 id="-">基本运算符</h1>
<hr>
<p>本页包含内容:</p>
<ul>
<li><a href="#terminology">术语</a></li>
<li><a href="#assignment_operator">赋值运算符</a></li>
<li><a href="#arithmetic_operators">数值运算符</a></li>
<li><a href="#compound_assignment_operators">组合赋值运算符(Compound Assignment Operators)</a></li>
<li><a href="#comparison_operators">比较运算符</a></li>
<li><a href="#ternary_conditional_operator">三目条件运算符(Ternary Conditional Operator)</a></li>
<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>&amp;&amp;</code>(如 <code>if enteredDoorCode &amp;&amp; passedRetinaScan</code>),还有自增运算符<code>++i</code>这样让自身加一的便捷运算。</p>
<p>Swift支持大部分标准C语言的运算符且改进许多特性来减少常规编码错误。如赋值符<code>=</code>不返回值,以防止错把等号<code>==</code>写成赋值号<code>=</code>而导致Bug。数值运算符(<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>)还提供了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>
<ul>
<li>一目运算符对单一操作对象操作,如<code>-a</code>。一目运算符分前置符和后置运算符,前置运算符需紧排操作对象之前,如<code>!b</code>,后置运算符需紧跟操作对象之后,如<code>i++</code></li>
<li>双目运算符操作两个操作对象,如 <code>2 + 3</code>。是中置的,因为它们出现在两个操作对象之间。</li>
<li>三目运算符操作三个操作对象和C语言一样Swift只有一个三目运算符就是三目条件运算符 <code>abc</code></li>
</ul>
<p>受运算符影响的值叫操作数,在表达式<code>1 + 2</code>中,加号<code>+</code>是双目运算符,它的两个操作数是值<code>1</code><code>2</code></p>
<p><a name="assignment_operator"></a></p>
<h2 id="-">赋值运算符</h2>
<p>赋值运算 <code>a = b</code>,表示用<code>b</code>的值来初始化或更新<code>a</code>的值。</p>
<pre><code class="lang-swift">let b = 10
var a = 5
a = b
// a 现在等于 10
</code></pre>
<p>如果赋值的右边是一个多元组,它的元素可以马上被分解多个变量或变量</p>
<pre><code>let (x, y) = (1, 2)
// 现在 x 等于 1, y 等于 2
</code></pre><p>与C语言和Objective-C不同Swift的赋值操作并不返回任何值。所以以下代码是错误的</p>
<pre><code class="lang-swift">if x = y {
// 此句错误, 因为 x = y 并不返回任何值
}
</code></pre>
<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>
<ul>
<li>加法<code>+</code></li>
<li>减法<code>-</code></li>
<li>乘法<code>*</code></li>
<li>除法<code>/</code></li>
</ul>
<pre><code class="lang-swift">1 + 2 // 等于 3
5 - 3 // 等于 2
2 * 3 // 等于 6
10.0 / 2.5 // 等于 4.0
</code></pre>
<p>与C语言和Objective-C不同的是Swift默认不允许在数值运算中出现溢出情况。但你可以使用Swift的溢出运算符来达到你有目的的溢出(如 <code>a &amp;+ b</code>)。详情请移步:<a href="23_Advanced_Operators.html#overflow_operators">溢出运算符</a></p>
<p>加法操作<code>+</code>也用于字符串的拼接:</p>
<pre><code class="lang-swift">&quot;hello, &quot; + &quot;world&quot; // 等于 &quot;hello, world&quot;
</code></pre>
<p>两个字符类型或一个字符类型和一个字符串类型,相加会生成一个新的字符串类型:</p>
<pre><code class="lang-swift">let dog: Character = &quot;d&quot;
let cow: Character = &quot;c&quot;
let dogCow = dog + cow
// 译者注: 原来的引号内是很可爱的小狗和小牛, 但win os下不支持表情字符, 所以改成了普通字符
// dogCow 现在是 &quot;dc&quot;
</code></pre>
<p>详细请点击<a href="03_Strings_and_Characters.html#concatenating_strings_and_characters">字符,字符串的拼接</a></p>
<h3 id="-">求余运算</h3>
<p>求余运算 <code>a % b</code> 是计算<code>b</code>的多少倍刚刚好可以容入<code>a</code>,多出来的那部分叫余数。</p>
<blockquote>
<p>注意:
求余运算(%)在其他语言也叫取模运算。然而严格说来,我们看该运算符对负数的操作结果,&quot;求余&quot;&quot;取模&quot;更合适些。</p>
</blockquote>
<p>我们来谈谈取余是怎么回事,计算<code>9 % 4</code>你先计算出4的多少倍会刚好可以容入<code>9</code>中。</p>
<p>2倍非常好那余数是1(用橙色标出)</p>
<p><img src="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Art/remainderInteger_2x.png" alt="Art/remainderInteger_2x.png" title="Art/remainderInteger_2x.png"></p>
<p>在Swift中这么来表达</p>
<pre><code class="lang-swift">9 % 4 // 等于 1
</code></pre>
<p>为了得到 <code>a % b</code> 的结果,<code>%</code>计算了以下等式,并输出<code>余数</code>作为结果:</p>
<p><em>a = (b × 倍数) + 余数</em></p>
<p><code>倍数</code>取最大值的时候,就会刚好可以容入<code>a</code>中。</p>
<p><code>9</code><code>4</code>代入等式中,我们得<code>1</code></p>
<pre><code class="lang-swift">9 = (4 × 2) + 1
</code></pre>
<p>同样的方法,我来们计算 <code>-9 % 4</code></p>
<pre><code>-9 % 4 // 等于 -1
</code></pre><p><code>-9</code><code>4</code>代入等式,<code>-2</code>是取到的最大整数:</p>
<pre><code class="lang-swift">-9 = (4 × -2) + -1
</code></pre>
<p>余数是<code>-1</code></p>
<p>在对负数<code>-b</code>求余时,<code>-b</code>的符号会被忽略。这意味着 <code>a % b</code><code>a % -b</code>的结果是相同的。</p>
<h3 id="-">浮点数求余计算</h3>
<p>不同于C和Objective-CSwift中是可以对浮点数进行求余的。</p>
<pre><code class="lang-swift">8 % 2.5 // 等于 0.5
</code></pre>
<p>这个例子中8除于2.5等于3余0.5所以结果是0.5。</p>
<p><img src="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Art/remainderFloat_2x.png" alt="Art/remainderFloat_2x.png" title="Art/remainderFloat_2x.png"></p>
<h3 id="-">自增和自增运算</h3>
<p>和C一样Swift也提供了方便对变量本身加1或减1的自增<code>++</code>和自减<code>--</code>的运算符。其操作对象可以是整形和浮点型。
</p>
<pre><code>var i = 0
++i // 现在 i = 1
</code></pre><p>每调用一次<code>++i</code><code>i</code>的值就会加1。
实际上,<code>++i</code><code>i = i + 1</code> 的简写,而<code>--i</code><code>i = i - 1</code>的简写。</p>
<p><code>++</code><code>--</code>既是前置又是后置运算。<code>++i</code><code>i++</code><code>--i</code><code>i--</code>都是有效的写法。</p>
<p>我们需要注意的是这些运算符修改了<code>i</code>后有一个返回值。如果你只想修改<code>i</code>的值,那你就可以忽略这个返回值。但如果你想使用返回值,你就需要留意前置和后置操作的返回值是不同的。</p>
<p><code>++</code>前置的时候,先自増再返回。</p>
<p><code>++</code>后置的时候,先返回再自增。</p>
<p>不懂?我们看例子:</p>
<pre><code class="lang-swift">var a = 0
let b = ++a // a 和 b 现在都是 1
let c = a++ // a 现在 2, 但 c 是 a 自增前的值 1
</code></pre>
<p>上述例子,<code>let b = ++a</code>,先把<code>a</code>加1了再返回<code>a</code>的值。所以<code>a</code><code>b</code>都是新值<code>1</code></p>
<p><code>let c = a++</code>,是先返回了<code>a</code>的值,然后<code>a</code>才加1。所以<code>c</code>得到了<code>a</code>的旧值1<code>a</code>加1后变成2。</p>
<p>除非你需要使用<code>i++</code>的特性,不然推荐你使用<code>++i</code><code>--i</code>,因为先修改后返回这样的行为更符合我们的逻辑。</p>
<h3 id="-">单目负号</h3>
<p>数值的正负号可以使用前缀<code>-</code>(即单目负号)来切换:</p>
<pre><code class="lang-swift">let three = 3
let minusThree = -three // minusThree 等于 -3
let plusThree = -minusThree // plusThree 等于 3, 或 &quot;负负3&quot;
</code></pre>
<p>单目负号写在操作数之前,中间没有空格。</p>
<h3 id="-">单目正号</h3>
<p>单目正号<code>+</code>不做任何改变地返回操作数的值。</p>
<pre><code class="lang-swift">let minusSix = -6
let alsoMinusSix = +minusSix // alsoMinusSix 等于 -6
</code></pre>
<p>虽然单目<code>+</code>做无用功,但当你在使用单目负号来表达负数时,你可以使用单目正号来表达正数,如此你的代码会具有对称美。</p>
<p><a name="compound_assignment_operators"></a></p>
<h2 id="-">复合赋值</h2>
<p>如同强大的C语言Swift也提供把其他运算符和赋值运算<code>=</code>组合的复合赋值运算符,加赋运算<code>+=</code>是其中一个例子:</p>
<pre><code class="lang-swift">var a = 1
a += 2 // a 现在是 3
</code></pre>
<p>表达式 <code>a += 2</code><code>a = a + 2</code> 的简写,一个加赋运算就把加法和赋值两件事完成了。</p>
<blockquote>
<p>注意:
复合赋值运算没有返回值,<code>let b = a += 2</code> 这类代码是错误。这不同于上面提到的自增和自减运算符。</p>
</blockquote>
<p><a href="../chapter3/04_Expressions.html">表达式</a>章节里有复合运算符的完整列表。
<a name="comparison_operators"></a></p>
<h2 id="-">比较运算</h2>
<p>所有标准C中的比较运算都可以在Swift中使用。</p>
<ul>
<li>等于 <code>a == b</code></li>
<li>不等于 <code>a= b</code></li>
<li>大于 <code>a &gt; b</code></li>
<li>小于 <code>a &lt; b</code></li>
<li>大于等于 <code>a &gt;= b</code></li>
<li>小于等于 <code>a &lt;= b</code></li>
</ul>
<blockquote>
<p>注意:
Swift也提供恒等<code>===</code>和不恒等<code>!==</code>这两个比较符来判断两个对象是否引用同一个对象实例。更多细节在<a href="09_Classes_and_Structures.html">类与结构</a></p>
</blockquote>
<p>每个比较运算都返回了一个标识表达式是否成立的布尔值:</p>
<pre><code class="lang-swift">1 == 1 // true, 因为 1 等于 1
2 != 1 // true, 因为 2 不等于 1
2 &gt; 1 // true, 因为 2 大于 1
1 &lt; 2 // true, 因为 1 小于2
1 &gt;= 1 // true, 因为 1 大于等于 1
2 &lt;= 1 // false, 因为 2 并不小于等于 1
</code></pre>
<p>比较运算多用于条件语句,如<code>if</code>条件:</p>
<pre><code class="lang-swift">let name = &quot;world&quot;
if name == &quot;world&quot; {
println(&quot;hello, world&quot;)
} else {
println(&quot;对不起, \(name), 我不认识你!&quot;)
}
// 输出 &quot;hello, world&quot;, 因为 `name` 就是等于 &quot;world&quot;
</code></pre>
<p>关于<code>if</code>语句,请看<a href="05_Control_Flow.html">控制流</a></p>
<p><a name="ternary_conditional_operator"></a></p>
<h2 id="-">三目条件运算</h2>
<p>三目条件运算的特殊在于它是有三个操作数的运算符,它的原型是 <code>问题答案1答案2</code>。它简洁地表达根据 <code>问题</code> 成立与否作出二选一的操作。如果 <code>问题</code> 成立,返回 <code>答案1</code> 的结果; 如果不成立,返回 <code>答案2</code> 的结果。</p>
<p>使用三目条件运算简化了以下代码:</p>
<pre><code class="lang-swift">if question: {
answer1
}
else {
answer2
}
</code></pre>
<p>这里有个计算表格行高的例子。如果有表头那行高应比内容高度要高出50像素; 如果没有表头只需高出20像素。</p>
<pre><code class="lang-swift">let contentHeight = 40
let hasHeader = true
let rowHeight = contentHeight + (hasHeader ? 50 : 20)
// rowHeight 现在是 90
</code></pre>
<p>这样写会比下面的代码简洁:</p>
<pre><code class="lang-swift">let contentHeight = 40
let hasHeader = true
var rowHeight = contentHeight
if hasHeader {
rowHeight = rowHeight + 50
} else {
rowHeight = rowHeight + 20
}
// rowHeight 现在是 90
</code></pre>
<p>第一段代码例子使用了三目条件运算,所以一行代码就能让我们得到正确答案。这比第二段代码简洁得多,无需将<code>rowHeight</code>定义成变量,因为它的值无需在<code>if</code>语句中改变。</p>
<p>三目条件运算提供有效率且便捷的方式来表达二选一的选择。需要注意的事,过度使用三目条件运算就会由简洁的代码变成难懂的代码。我们应避免在一个组合语句使用多个三目条件运算符。</p>
<p><a name="range_operators"></a></p>
<h2 id="-">区间运算符</h2>
<p>Swift提供了两个方便表达一个区间的值的运算符。</p>
<h3 id="-">闭区间运算符</h3>
<p>闭区间运算符<code>a...b</code>定义一个包含从<code>a</code><code>b</code>(包括<code>a</code><code>b</code>)的所有值的区间。
闭区间运算符在迭代一个区间的所有值时是非常有用的,如在<code>for-in</code>循环中:</p>
<pre><code class="lang-swift">for index in 1...5 {
println(&quot;\(index) * 5 = \(index * 5)&quot;)
}
// 1 * 5 = 5
// 2 * 5 = 10
// 3 * 5 = 15
// 4 * 5 = 20
// 5 * 5 = 25
</code></pre>
<p>关于<code>for-in</code>,请看<a href="05_Control_Flow.html">控制流</a></p>
<h3 id="-">半闭区间</h3>
<p>半闭区间<code>a..b</code>定义一个从<code>a</code><code>b</code>但不包括<code>b</code>的区间。
之所以称为半闭区间,是因为该区间包含第一个值而不包括最后的值。</p>
<p>半闭区间的实用性在于当你使用一个0始的列表(如数组)时非常方便地从0数到列表的长度。</p>
<pre><code class="lang-swift">let names = [&quot;Anna&quot;, &quot;Alex&quot;, &quot;Brian&quot;, &quot;Jack&quot;]
let count = names.count
for i in 0..count {
println(&quot;第 \(i + 1) 个人叫 \(names[i])&quot;)
}
// 第 1 个人叫 Anna
// 第 2 个人叫 Alex
// 第 3 个人叫 Brian
// 第 4 个人叫 Jack
</code></pre>
<blockquote>
<p>注意:
数组有4个元素<code>0..count</code>只数到 3(最后一个元素的下标),因为它是半闭区间。关于数组,请查阅<a href="04_Collection_Types.html#arrays">数组</a></p>
</blockquote>
<p><a name="logical_operators"></a></p>
<h2 id="-">逻辑运算</h2>
<p>逻辑运算的操作对象是逻辑布尔值。Swift支持基于C语言的三个标准逻辑运算。</p>
<ul>
<li>逻辑非<code>!a</code></li>
<li>逻辑与 <code>a &amp;&amp; b</code></li>
<li>逻辑或 <code>a || b</code></li>
</ul>
<h3 id="-">逻辑非</h3>
<p>逻辑非运算<code>!a</code>对一个布尔值取反,使得<code>true</code><code>false</code><code>false</code><code>true</code></p>
<p>它是一个前置运算符,需出现在操作数之前,且不加空格。读作 <code>非 a</code>,然后我们看以下例子:</p>
<pre><code class="lang-swift">let allowedEntry = false
if !allowedEntry {
println(&quot;ACCESS DENIED&quot;)
}
// prints &quot;ACCESS DENIED&quot;
</code></pre>
<p><code>ifallowedEntry</code>语句可以读作 &quot;如果 非 alowed entry。&quot;,接下一行代码只有在如果 &quot;非 allow entry&quot;<code>true</code>,即<code>allowEntry</code><code>false</code>时被执行。</p>
<p>在示例代码中,小心地选择布尔常量或变量有助于代码的可读性,并且避免使用双重逻辑非运算,或混乱的逻辑语句。</p>
<h3 id="-">逻辑与</h3>
<p>逻辑与 <code>a &amp;&amp; b</code> 表达了只有<code>a</code><code>b</code>的值都为<code>true</code>时,整个表达式的值才会是<code>true</code></p>
<p>只要任意一个值为<code>false</code>,整个表达式的值就为<code>false</code>。事实上,如果第一个值为<code>false</code>,那么是不去计算第二个值的,因为它已经不可能影响整个表达式的结果了。这被称做 &quot;短路计算&quot;</p>
<p>以下例子,只有两个值都为值的时候才允许进入:</p>
<pre><code class="lang-swift">let enteredDoorCode = true
let passedRetinaScan = false
if enteredDoorCode &amp;&amp; passedRetinaScan {
println(&quot;Welcome!&quot;)
} else {
println(&quot;ACCESS DENIED&quot;)
}
// 输出 &quot;ACCESS DENIED
</code></pre>
<h3 id="-">逻辑或</h3>
<p>逻辑或 <code>a || b</code> 是一个由两个连续的<code>|</code>组成的中置运算符。它表示了两个逻辑表达式的其中一个为<code>true</code>,整个表达式就为<code>true</code></p>
<p>同逻辑与运算类似,逻辑或也是&quot;短路计算&quot;的,当左端的表达式为<code>true</code>时,将不计算右边的表达式了,因为它不可能改变整个表达式的值了。</p>
<p>以下示例代码中,第一个布尔值<code>hasDoorKey</code><code>false</code>,但第二个值<code>knowsOverridePassword</code><code>true</code>,所以整个表达是<code>true</code>,于是允许进入:</p>
<pre><code class="lang-swift">let hasDoorKey = false
let knowsOverridePassword = true
if hasDoorKey || knowsOverridePassword {
println(&quot;Welcome!&quot;)
} else {
println(&quot;ACCESS DENIED&quot;)
}
// 输出 &quot;Welcome!&quot;
</code></pre>
<h3 id="-">组合逻辑</h3>
<p>我们可以组合多个逻辑运算来表达一个复合逻辑:</p>
<pre><code class="lang-swift">if enteredDoorCode &amp;&amp; passedRetinaScan || hasDoorKey || knowsOverridePassword {
println(&quot;Welcome!&quot;)
} else {
println(&quot;ACCESS DENIED&quot;)
}
// 输出 &quot;Welcome!&quot;
</code></pre>
<p>这个例子使用了含多个<code>&amp;&amp;</code><code>||</code>的复合逻辑。但无论怎样,<code>&amp;&amp;</code><code>||</code>始终只能操作两个值。所以这实际是三个简单逻辑连续操作的结果。我们来解读一下:</p>
<p>如果我们输入了正确的密码并通过了视网膜扫描; 或者我们有一把有效的钥匙; 又或者我们知道紧急情况下重置的密码,我们就能把门打开进入。</p>
<p>前两种情况,我们都不满足,所以前两个简单逻辑的结果是<code>false</code>,但是我们是知道紧急情况下重置的密码的,所以整个复杂表达式的值还是<code>true</code></p>
<h3 id="-">使用括号来明确优先级</h3>
<p>为了一个复杂表达式更容易读懂,在合适的地方使用括号来明确优先级是很有效的,虽然它并非必要的。在上个关于门的权限的例子中,我们给第一个部分加个括号,使用它看起来逻辑更明确。</p>
<pre><code class="lang-swift">if (enteredDoorCode &amp;&amp; passedRetinaScan) || hasDoorKey || knowsOverridePassword {
println(&quot;Welcome!&quot;)
} else {
println(&quot;ACCESS DENIED&quot;)
}
// prints &quot;Welcome!&quot;
</code></pre>
<p>这括号使得前两个值被看成整个逻辑表达中独立的一个部分。虽然有括号和没括号的输出结果是一样的,但对于读代码的人来说有括号的代码更清晰。</p>
<p>可读性比简洁性更重要,请在可以让你代码变清晰地地方加个括号吧!</p>
</section>
</div>
</div>
</div>
<a href="../chapter2/01_The_Basics.html" class="navigation navigation-prev " aria-label="Previous page: 基础部分"><i class="fa fa-angle-left"></i></a>
<a href="../chapter2/03_Strings_and_Characters.html" class="navigation navigation-next " aria-label="Next page: 字符串和字符"><i class="fa fa-angle-right"></i></a>
</div>
</div>
<script src="http://cdn.bootcss.com/ace/1.1.3/ace.js"></script>
<script src="http://cdn.bootcss.com/ace/1.1.3/mode-javascript.js"></script>
<script src="../gitbook/jsrepl/jsrepl.js" id="jsrepl-script"></script>
<script src="../gitbook/app.js"></script>
<script src="../gitbook/plugins/gitbook-plugin-mixpanel/plugin.js"></script>
<script src="http://cdn.mathjax.org/mathjax/2.0-latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script src="../gitbook/plugins/gitbook-plugin-mathjax/plugin.js"></script>
<script>
require(["gitbook"], function(gitbook) {
var config = {};
gitbook.start(config);
});
</script>
<script src="http://s19.cnzz.com/z_stat.php?id=1000480711&web_id=1000480711" language="JavaScript"></script></body>
</html>