947 lines
52 KiB
HTML
947 lines
52 KiB
HTML
<!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>《The Swift Programming Language》中文版</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 中文翻译组:364279588(要求对翻译感兴趣)">
|
||
<meta name="keywords" content="gitbook,github" >
|
||
<meta name="generator" content="www.gitbook.io">
|
||
|
||
|
||
<link rel="next" href="../chapter3/chapter3.html" />
|
||
|
||
|
||
<link rel="prev" href="../chapter2/22_Generics.html" />
|
||
|
||
|
||
<meta property="og:title" content="高级操作符 | The Swift Programming Language 中文版">
|
||
<meta property="og:site_name" content="The Swift Programming Language 中文版">
|
||
<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.23" data-basepath=".." data-revision="1402674872336">
|
||
<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="../" >The Swift Programming Language 中文版</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> 序</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><li><p style="padding-left:5px;padding-right:5px;">翻译无任何商业目的,仅供内部学习交流使用!</p></li><li><script src="http://s19.cnzz.com/z_stat.php?id=1000480711&web_id=1000480711" language="JavaScript"></script></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: 52.63157894736842%;min-width: 50%;"></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 done " data-progress="2.20" style="left: 44.73684210526316%;"></a>
|
||
|
||
<a href="../chapter2/21_Protocols.html" title="协议" class="chapter done " data-progress="2.21" style="left: 47.36842105263158%;"></a>
|
||
|
||
<a href="../chapter2/22_Generics.html" title="泛型" class="chapter done " data-progress="2.22" style="left: 50%;"></a>
|
||
|
||
<a href="../chapter2/23_Advanced_Operators.html" title="高级操作符" class="chapter done " 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_244">
|
||
|
||
<blockquote>
|
||
<p>翻译:xielingwang</p>
|
||
<p>校对:numbbbbb</p>
|
||
</blockquote>
|
||
<h1 id="-">高级运算符</h1>
|
||
<hr>
|
||
<p>本页内容包括:</p>
|
||
<ul>
|
||
<li><a href="#bitwise_operators">位运算符</a></li>
|
||
<li><a href="#overflow_operators">溢出运算符</a></li>
|
||
<li><a href="#precedence_and_associativity">优先级和结合性(Precedence and Associativity)</a></li>
|
||
<li><a href="#operator_functions">运算符函数(Operator Functions)</a></li>
|
||
<li><a href="#custom_operators">自定义运算符</a></li>
|
||
</ul>
|
||
<p>除了<a href="02_Basic_Operators.html">基本操作符</a>中所讲的运算符,Swift还有许多复杂的高级运算符,包括了C语和Objective-C中的位运算符和移位运算。</p>
|
||
<p>不同于C语言中的数值计算,Swift的数值计算默认是不可溢出的。溢出行为会被捕获并报告为错误。你是故意的?好吧,你可以使用Swift为你准备的另一套默认允许溢出的数值运算符,如可溢出加<code>&+</code>。所有允许溢出的运算符都是以<code>&</code>开始的。</p>
|
||
<p>自定义的结构,类和枚举,是否可以使用标准的运算符来定义操作?当然可以!在Swift中,你可以为你创建的所有类型定制运算符的操作。</p>
|
||
<p>可定制的运算符并不限于那些预设的运算符,自定义有个性的中置,前置,后置及赋值运算符,当然还有优先级和结合性。这些运算符的实现可以运用预设的运算符,也可以运用之前定制的运算符。</p>
|
||
<p><a name="bitwise_operators"></a></p>
|
||
<h2 id="-">位运算符</h2>
|
||
<p>位操作符通常在诸如图像处理和创建设备驱动等底层开发中使用,使用它可以单独操作数据结构中原始数据的比特位。在使用一个自定义的协议进行通信的时候,运用位运算符来对原始数据进行编码和解码也是非常有效的。</p>
|
||
<p>Swift支持如下所有C语言的位运算符:</p>
|
||
<h3 id="-">按位取反运算符</h3>
|
||
<p>按位取反运算符<code>~</code>对一个操作数的每一位都取反。</p>
|
||
<p><img src="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Art/bitwiseNOT_2x.png" alt="Art/bitwiseNOT_2x.png" title="Art/bitwiseNOT_2x.png"></p>
|
||
<p>这个运算符是前置的,所以请不加任何空格地写着操作数之前。</p>
|
||
<pre><code class="lang-swift">let initialBits: UInt8 = 0b00001111
|
||
let invertedBits = ~initialBits // 等于 0b11110000
|
||
</code></pre>
|
||
<p><code>UInt8</code>是8位无符整型,可以存储0~255之间的任意数。这个例子初始化一个整型为二进制值<code>00001111</code>(前4位为<code>0</code>,后4位为<code>1</code>),它的十进制值为<code>15</code>。</p>
|
||
<p>使用按位取反运算<code>~</code>对<code>initialBits</code>操作,然后赋值给<code>invertedBits</code>这个新常量。这个新常量的值等于所有位都取反的<code>initialBits</code>,即<code>1</code>变成<code>0</code>,<code>0</code>变成<code>1</code>,变成了<code>11110000</code>,十进制值为<code>240</code>。</p>
|
||
<h3 id="-">按位与运算符</h3>
|
||
<p>按位与运算符对两个数进行操作,然后返回一个新的数,这个数的每个位都需要两个输入数的同一位都为1时才为1。</p>
|
||
<p><img src="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Art/bitwiseAND_2x.png" alt="Art/bitwiseAND_2x.png" title="Art/bitwiseAND_2x.png"></p>
|
||
<p>以下代码,<code>firstSixBits</code>和<code>lastSixBits</code>中间4个位都为1。对它俩进行按位与运算后,就得到了<code>00111100</code>,即十进制的<code>60</code>。</p>
|
||
<pre><code class="lang-swift">let firstSixBits: UInt8 = 0b11111100
|
||
let lastSixBits: UInt8 = 0b00111111
|
||
let middleFourBits = firstSixBits & lastSixBits // 等于 00111100
|
||
</code></pre>
|
||
<h3 id="-">按位或运算</h3>
|
||
<p>按位或运算符<code>|</code>比较两个数,然后返回一个新的数,这个数的每一位设置1的条件是两个输入数的同一位都不为0(即任意一个为1,或都为1)。</p>
|
||
<p><img src="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Art/bitwiseOR_2x.png" alt="Art/bitwiseOR_2x.png" title="Art/bitwiseOR_2x.png"></p>
|
||
<p>如下代码,<code>someBits</code>和<code>moreBits</code>在不同位上有<code>1</code>。按位或运行的结果是<code>11111110</code>,即十进制的<code>254</code>。</p>
|
||
<pre><code class="lang-swift">let someBits: UInt8 = 0b10110010
|
||
let moreBits: UInt8 = 0b01011110
|
||
let combinedbits = someBits | moreBits // 等于 11111110
|
||
</code></pre>
|
||
<h3 id="-">按位异或运算符</h3>
|
||
<p>按位异或运算符<code>^</code>比较两个数,然后返回一个数,这个数的每个位设为<code>1</code>的条件是两个输入数的同一位不同,如果相同就设为<code>0</code>。</p>
|
||
<p><img src="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Art/bitwiseXOR_2x.png" alt="Art/bitwiseXOR_2x.png" title="Art/bitwiseXOR_2x.png"></p>
|
||
<p>以下代码,<code>firstBits</code>和<code>otherBits</code>都有一个<code>1</code>跟另一个数不同的。所以按位异或的结果是把它这些位置为<code>1</code>,其他都置为<code>0</code>。</p>
|
||
<pre><code class="lang-swift">let firstBits: UInt8 = 0b00010100
|
||
let otherBits: UInt8 = 0b00000101
|
||
let outputBits = firstBits ^ otherBits // 等于 00010001
|
||
</code></pre>
|
||
<h3 id="-">按位左移/右移运算符</h3>
|
||
<p>左移运算符<code><<</code>和右移运算符<code>>></code>会把一个数的所有比特位按以下定义的规则向左或向右移动指定位数。</p>
|
||
<p>按位左移和按位右移的效果相当把一个整数乘于或除于一个因子为<code>2</code>的整数。向左移动一个整型的比特位相当于把这个数乘于<code>2</code>,向右移一位就是除于<code>2</code>。</p>
|
||
<h4 id="-">无符整型的移位操作</h4>
|
||
<p>对无符整型的移位的效果如下:</p>
|
||
<p>已经存在的比特位向左或向右移动指定的位数。被移出整型存储边界的的位数直接抛弃,移动留下的空白位用零<code>0</code>来填充。这种方法称为逻辑移位。</p>
|
||
<p>以下这张把展示了 <code>11111111 << 1</code>(<code>11111111</code>向左移1位),和 <code>11111111 >> 1</code>(<code>11111111</code>向右移1位)。蓝色的是被移位的,灰色是被抛弃的,橙色的<code>0</code>是被填充进来的。</p>
|
||
<p><img src="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Art/bitshiftUnsigned_2x.png" alt="Art/bitshiftUnsigned_2x.png" title="Art/bitshiftUnsigned_2x.png"></p>
|
||
<pre><code class="lang-swift">let shiftBits: UInt8 = 4 // 即二进制的00000100
|
||
shiftBits << 1 // 00001000
|
||
shiftBits << 2 // 00010000
|
||
shiftBits << 5 // 10000000
|
||
shiftBits << 6 // 00000000
|
||
shiftBits >> 2 // 00000001
|
||
</code></pre>
|
||
<p>你可以使用移位操作进行其他数据类型的编码和解码。</p>
|
||
<pre><code class="lang-swift">let pink: UInt32 = 0xCC6699
|
||
let redComponent = (pink & 0xFF0000) >> 16 // redComponent 是 0xCC, 即 204
|
||
let greenComponent = (pink & 0x00FF00) >> 8 // greenComponent 是 0x66, 即 102
|
||
let blueComponent = pink & 0x0000FF // blueComponent 是 0x99, 即 153
|
||
</code></pre>
|
||
<p>这个例子使用了一个<code>UInt32</code>的命名为<code>pink</code>的常量来存储层叠样式表<code>CSS</code>中粉色的颜色值,<code>CSS</code>颜色<code>#CC6699</code>在Swift用十六进制<code>0xCC6699</code>来表示。然后使用按位与(&)和按位右移就可以从这个颜色值中解析出红(CC),绿(66),蓝(99)三个部分。</p>
|
||
<p>对<code>0xCC6699</code>和<code>0xFF0000</code>进行按位与<code>&</code>操作就可以得到红色部分。<code>0xFF0000</code>中的<code>0</code>了遮盖了<code>OxCC6699</code>的第二和第三个字节,这样<code>6699</code>被忽略了,只留下<code>0xCC0000</code>。</p>
|
||
<p>然后,按向右移动16位,即 <code>>> 16</code>。十六进制中每两个字符是8比特位,所以移动16位的结果是把<code>0xCC0000</code>变成<code>0x0000CC</code>。这和<code>0xCC</code>是相等的,都是十进制的<code>204</code>。</p>
|
||
<p>同样的,绿色部分来自于<code>0xCC6699</code>和<code>0x00FF00</code>的按位操作得到<code>0x006600</code>。然后向右移动8們,得到<code>0x66</code>,即十进制的<code>102</code>。</p>
|
||
<p>最后,蓝色部分对<code>0xCC6699</code>和<code>0x0000FF</code>进行按位与运算,得到<code>0x000099</code>,无需向右移位了,所以结果就是<code>0x99</code>,即十进制的<code>153</code>。</p>
|
||
<h4 id="-">有符整型的移位操作</h4>
|
||
<p>有符整型的移位操作相对复杂得多,因为正负号也是用二进制位表示的。(这里举的例子虽然都是8位的,但它的原理是通用的。)</p>
|
||
<p>有符整型通过第1个比特位(称为符号位)来表达这个整数是正数还是负数。<code>0</code>代表正数,<code>1</code>代表负数。</p>
|
||
<p>其余的比特位(称为数值位)存储其实值。有符正整数和无符正整数在计算机里的存储结果是一样的,下来我们来看<code>+4</code>内部的二进制结构。</p>
|
||
<p><img src="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Art/bitshiftSignedFour_2x.png" alt="Art/bitshiftSignedFour_2x.png" title="Art/bitshiftSignedFour_2x.png"></p>
|
||
<p>符号位为<code>0</code>,代表正数,另外7比特位二进制表示的实际值就刚好是<code>4</code>。</p>
|
||
<p>负数呢,跟正数不同。负数存储的是2的n次方减去它的绝对值,n为数值位的位数。一个8比特的数有7个数值位,所以是2的7次方,即128。</p>
|
||
<p>我们来看<code>-4</code>存储的二进制结构。</p>
|
||
<p><img src="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Art/bitshiftSignedMinusFour_2x.png" alt="Art/bitshiftSignedMinusFour_2x.png" title="Art/bitshiftSignedMinusFour_2x.png"></p>
|
||
<p>现在符号位为<code>1</code>,代表负数,7个数值位要表达的二进制值是124,即128 - 4。</p>
|
||
<p><img src="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Art/bitshiftSignedMinusFourValue_2x.png" alt="Art/bitshiftSignedMinusFourValue_2x.png" title="Art/bitshiftSignedMinusFourValue_2x.png"></p>
|
||
<p>负数的编码方式称为二进制补码表示。这种表示方式看起来很奇怪,但它有几个优点。</p>
|
||
<p>首先,只需要对全部8个比特位(包括符号)做标准的二进制加法就可以完成 <code>-1 + -4</code> 的操作,忽略加法过程产生的超过8个比特位表达的任何信息。</p>
|
||
<p><img src="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Art/bitshiftSignedAddition_2x.png" alt="Art/bitshiftSignedAddition_2x.png" title="Art/bitshiftSignedAddition_2x.png"></p>
|
||
<p>第二,由于使用二进制补码表示,我们可以和正数一样对负数进行按位左移右移的,同样也是左移1位时乘于<code>2</code>,右移1位时除于<code>2</code>。要达到此目的,对有符整型的右移有一个特别的要求:</p>
|
||
<p>对有符整型按位右移时,使用符号位(正数为<code>0</code>,负数为<code>1</code>)填充空白位。</p>
|
||
<p><img src="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Art/bitshiftSigned_2x.png" alt="Art/bitshiftSigned_2x.png" title="Art/bitshiftSigned_2x.png"></p>
|
||
<p>这就确保了在右移的过程中,有符整型的符号不会发生变化。这称为算术移位。</p>
|
||
<p>正因为正数和负数特殊的存储方式,向右移位使它接近于<code>0</code>。移位过程中保持符号会不变,负数在接近<code>0</code>的过程中一直是负数。</p>
|
||
<p><a name="overflow_operators"></a></p>
|
||
<h2 id="-">溢出运算符</h2>
|
||
<p>默认情况下,当你往一个整型常量或变量赋于一个它不能承载的大数时,Swift不会让你这么干的,它会报错。这样,在操作过大或过小的数的时候就很安全了。</p>
|
||
<p>例如,<code>Int16</code>整型能承载的整数范围是<code>-32768</code>到<code>32767</code>,如果给它赋上超过这个范围的数,就会报错:</p>
|
||
<pre><code class="lang-swift">var potentialOverflow = Int16.max
|
||
// potentialOverflow 等于 32767, 这是 Int16 能承载的最大整数
|
||
potentialOverflow += 1
|
||
// 噢, 出错了
|
||
</code></pre>
|
||
<p>对过大或过小的数值进行错误处理让你的数值边界条件更灵活。</p>
|
||
<p>当然,你有意在溢出时对有效位进行截断,你可采用溢出运算,而非错误处理。Swfit为整型计算提供了5个<code>&</code>符号开头的溢出运算符。</p>
|
||
<ul>
|
||
<li>溢出加法 <code>&+</code></li>
|
||
<li>溢出减法 <code>&-</code></li>
|
||
<li>溢出乘法 <code>&*</code></li>
|
||
<li>溢出除法 <code>&/</code></li>
|
||
<li>溢出求余 <code>&%</code></li>
|
||
</ul>
|
||
<h3 id="-">值的上溢出</h3>
|
||
<p>下面例子使用了溢出加法<code>&+</code>来解剖的无符整数的上溢出</p>
|
||
<pre><code class="lang-swift">var willOverflow = UInt8.max
|
||
// willOverflow 等于UInt8的最大整数 255
|
||
willOverflow = willOverflow &+ 1
|
||
// 这时候 willOverflow 等于 0
|
||
</code></pre>
|
||
<p><code>willOverflow</code>用<code>Int8</code>所能承载的最大值<code>255</code>(二进制<code>11111111</code>),然后用<code>&+</code>加1。然后<code>UInt8</code>就无法表达这个新值的二进制了,也就导致了这个新值上溢出了,大家可以看下图。溢出后,新值在<code>UInt8</code>的承载范围内的那部分是<code>00000000</code>,也就是<code>0</code>。</p>
|
||
<p><img src="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Art/overflowAddition_2x.png" alt="Art/overflowAddition_2x.png" title="Art/overflowAddition_2x.png"></p>
|
||
<h3 id="-">值的下溢出</h3>
|
||
<p>数值也有可能因为太小而越界。举个例子:</p>
|
||
<p><code>UInt8</code>的最小值是<code>0</code>(二进制为<code>00000000</code>)。使用<code>&-</code>进行溢出减1,就会得到二进制的<code>11111111</code>即十进制的<code>255</code>。</p>
|
||
<p><img src="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Art/overflowUnsignedSubtraction_2x.png" alt="Art/overflowUnsignedSubtraction_2x.png" title="Art/overflowUnsignedSubtraction_2x.png"></p>
|
||
<p>Swift代码是这样的:</p>
|
||
<pre><code class="lang-swift">var willUnderflow = UInt8.min
|
||
// willUnderflow 等于UInt8的最小值0
|
||
willUnderflow = willUnderflow &- 1
|
||
// 此时 willUnderflow 等于 255
|
||
</code></pre>
|
||
<p>有符整型也有类似的下溢出,有符整型所有的减法也都是对包括在符号位在内的二进制数进行二进制减法的,这在 "按位左移/右移运算符" 一节提到过。最小的有符整数是<code>-128</code>,即二进制的<code>10000000</code>。用溢出减法减去去1后,变成了<code>01111111</code>,即UInt8所能承载的最大整数<code>127</code>。</p>
|
||
<p><img src="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Art/overflowSignedSubtraction_2x.png" alt="Art/overflowSignedSubtraction_2x.png" title="Art/overflowSignedSubtraction_2x.png"></p>
|
||
<p>来看看Swift代码:</p>
|
||
<pre><code class="lang-swift">var signedUnderflow = Int8.min
|
||
// signedUnderflow 等于最小的有符整数 -128
|
||
signedUnderflow = signedUnderflow &- 1
|
||
// 如今 signedUnderflow 等于 127
|
||
</code></pre>
|
||
<h3 id="-">除零溢出</h3>
|
||
<p>一个数除于0 <code>i / 0</code>,或者对0求余数 <code>i % 0</code>,就会产生一个错误。</p>
|
||
<pre><code class="lang-swift">let x = 1
|
||
let y = x / 0
|
||
</code></pre>
|
||
<p>使用它们对应的可溢出的版本的运算符<code>&/</code>和<code>&%</code>进行除0操作时就会得到<code>0</code>值。</p>
|
||
<pre><code class="lang-swift">let x = 1
|
||
let y = x &/ 0
|
||
// y 等于 0
|
||
</code></pre>
|
||
<p><a name="precedence_and_associativity"></a></p>
|
||
<h2 id="-">优先级和结合性</h2>
|
||
<p>运算符的优先级使得一些运算符优先于其他运算符,高优先级的运算符会先被计算。</p>
|
||
<p>结合性定义相同优先级的运算符在一起时是怎么组合或关联的,是和左边的一组呢,还是和右边的一组。意思就是,到底是和左边的表达式结合呢,还是和右边的表达式结合?</p>
|
||
<p>在混合表达式中,运算符的优先级和结合性是非常重要的。举个例子,为什么下列表达式的结果为<code>4</code>?</p>
|
||
<pre><code class="lang-swift">2 + 3 * 4 % 5
|
||
// 结果是 4
|
||
</code></pre>
|
||
<p>如果严格地从左计算到右,计算过程会是这样:</p>
|
||
<ul>
|
||
<li>2 plus 3 equals 5;</li>
|
||
<li>2 + 3 = 5</li>
|
||
<li>5 times 4 equals 20;</li>
|
||
<li>5 * 4 = 20</li>
|
||
<li>20 remainder 5 equals 0</li>
|
||
<li>20 / 5 = 4 余 0</li>
|
||
</ul>
|
||
<p>但是正确答案是<code>4</code>而不是<code>0</code>。优先级高的运算符要先计算,在Swift和C语言中,都是先乘除后加减的。所以,执行完乘法和求余运算才能执行加减运算。</p>
|
||
<p>乘法和求余拥有相同的优先级,在运算过程中,我们还需要结合性,乘法和求余运算都是左结合的。这相当于在表达式中有隐藏的括号让运算从左开始。</p>
|
||
<pre><code class="lang-swift">2 + ((3 * 4) % 5)
|
||
</code></pre>
|
||
<p>(3 <em> 4) is 12, so this is equivalent to:
|
||
3 </em> 4 = 12,所以这相当于:</p>
|
||
<pre><code class="lang-swift">2 + (12 % 5)
|
||
</code></pre>
|
||
<p>(12 % 5) is 2, so this is equivalent to:
|
||
12 % 5 = 2,所这又相当于</p>
|
||
<pre><code class="lang-swift">2 + 2
|
||
</code></pre>
|
||
<p>计算结果为 4。</p>
|
||
<p>查阅Swift运算符的优先级和结合性的完整列表,请看<a href="../chapter3/04_Expressions.html">表达式</a>。</p>
|
||
<blockquote>
|
||
<p>注意:</p>
|
||
<p>Swift的运算符较C语言和Objective-C来得更简单和保守,这意味着跟基于C的语言可能不一样。所以,在移植已有代码到Swift时,注意去确保代码按你想的那样去执行。</p>
|
||
</blockquote>
|
||
<p><a name="operator_functions"></a></p>
|
||
<h2 id="-">运算符函数</h2>
|
||
<p>让已有的运算符也可以对自定义的类和结构进行运算,这称为运算符重载。</p>
|
||
<p>这个例子展示了如何用<code>+</code>让一个自定义的结构做加法。算术运算符<code>+</code>是一个两目运算符,因为它有两个操作数,而且它必须出现在两个操作数之间。</p>
|
||
<p>例子中定义了一个名为<code>Vector2D</code>的二维坐标向量 <code>(x,y)</code> 的结构,然后定义了让两个<code>Vector2D</code>的对象相加的运算符函数。</p>
|
||
<pre><code class="lang-swift">struct Vector2D {
|
||
var x = 0.0, y = 0.0
|
||
}
|
||
@infix func + (left: Vector2D, right: Vector2D) -> Vector2D {
|
||
return Vector2D(x: left.x + right.x, y: left.y + right.y)
|
||
}
|
||
</code></pre>
|
||
<p>该运算符函数定义了一个全局的<code>+</code>函数,这个函数需要两个<code>Vector2D</code>类型的参数,返回值也是<code>Vector2D</code>类型。需要定义和实现一个中置运算的时候,在关键字<code>func</code>之前写上属性 <code>@infix</code> 就可以了。</p>
|
||
<p>在这个代码实现中,参数被命名为了<code>left</code>和<code>right</code>,代表<code>+</code>左边和右边的两个<code>Vector2D</code>对象。函数返回了一个新的<code>Vector2D</code>的对象,这个对象的<code>x</code>和<code>y</code>分别等于两个参数对象的<code>x</code>和<code>y</code>的和。</p>
|
||
<p>这个函数是全局的,而不是<code>Vector2D</code>结构的成员方法,所以任意两个<code>Vector2D</code>对象都可以使用这个中置运算符。</p>
|
||
<pre><code class="lang-swift">let vector = Vector2D(x: 3.0, y: 1.0)
|
||
let anotherVector = Vector2D(x: 2.0, y: 4.0)
|
||
let combinedVector = vector + anotherVector
|
||
// combinedVector 是一个新的Vector2D, 值为 (5.0, 5.0)
|
||
</code></pre>
|
||
<p>这个例子实现两个向量 <code>(3.0,1.0)</code> 和 <code>(2.0,4.0)</code> 相加,得到向量 <code>(5.0,5.0)</code> 的过程。如下图示:</p>
|
||
<p><img src="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Art/vectorAddition_2x.png" alt="Art/vectorAddition_2x.png" title="Art/vectorAddition_2x.png"></p>
|
||
<h3 id="-">前置和后置运算符</h3>
|
||
<p>上个例子演示了一个双目中置运算符的自定义实现,同样我们也可以玩标准单目运算符的实现。单目运算符只有一个操作数,在操作数之前就是前置的,如<code>-a</code>; 在操作数之后就是后置的,如<code>i++</code>。</p>
|
||
<p>实现一个前置或后置运算符时,在定义该运算符的时候于关键字<code>func</code>之前标注 <code>@prefix</code> 或 <code>@postfix</code> 属性。</p>
|
||
<pre><code class="lang-swift">@prefix func - (vector: Vector2D) -> Vector2D {
|
||
return Vector2D(x: -vector.x, y: -vector.y)
|
||
}
|
||
</code></pre>
|
||
<p>这段代码为<code>Vector2D</code>类型提供了单目减运算<code>-a</code>,<code>@prefix</code>属性表明这是个前置运算符。</p>
|
||
<p>对于数值,单目减运算符可以把正数变负数,把负数变正数。对于<code>Vector2D</code>,单目减运算将其<code>x</code>和<code>y</code>都进进行单目减运算。</p>
|
||
<pre><code class="lang-swift">let positive = Vector2D(x: 3.0, y: 4.0)
|
||
let negative = -positive
|
||
// negative 为 (-3.0, -4.0)
|
||
let alsoPositive = -negative
|
||
// alsoPositive 为 (3.0, 4.0)
|
||
</code></pre>
|
||
<h3 id="-">组合赋值运算符</h3>
|
||
<p>组合赋值是其他运算符和赋值运算符一起执行的运算。如<code>+=</code>把加运算和赋值运算组合成一个操作。实现一个组合赋值符号需要使用<code>@assignment</code>属性,还需要把运算符的左参数设置成<code>inout</code>,因为这个参数会在运算符函数内直接修改它的值。</p>
|
||
<pre><code class="lang-swift">@assignment func += (inout left: Vector2D, right: Vector2D) {
|
||
left = left + right
|
||
}
|
||
</code></pre>
|
||
<p>因为加法运算在之前定义过了,这里无需重新定义。所以,加赋运算符函数使用已经存在的高级加法运算符函数来执行左值加右值的运算。</p>
|
||
<pre><code class="lang-swift">var original = Vector2D(x: 1.0, y: 2.0)
|
||
let vectorToAdd = Vector2D(x: 3.0, y: 4.0)
|
||
original += vectorToAdd
|
||
// original 现在为 (4.0, 6.0)
|
||
</code></pre>
|
||
<p>你可以将 <code>@assignment</code> 属性和 <code>@prefix</code> 或 <code>@postfix</code> 属性起来组合,实现一个<code>Vector2D</code>的前置运算符。</p>
|
||
<pre><code class="lang-swift">@prefix @assignment func ++ (inout vector: Vector2D) -> Vector2D {
|
||
vector += Vector2D(x: 1.0, y: 1.0)
|
||
return vector
|
||
}
|
||
</code></pre>
|
||
<p>这个前置使用了已经定义好的高级加赋运算,将自己加上一个值为 <code>(1.0,1.0)</code> 的对象然后赋给自己,然后再将自己返回。</p>
|
||
<pre><code class="lang-swift">var toIncrement = Vector2D(x: 3.0, y: 4.0)
|
||
let afterIncrement = ++toIncrement
|
||
// toIncrement 现在是 (4.0, 5.0)
|
||
// afterIncrement 现在也是 (4.0, 5.0)
|
||
</code></pre>
|
||
<blockquote>
|
||
<p>注意:</p>
|
||
<p>默认的赋值符是不可重载的。只有组合赋值符可以重载。三目条件运算符 <code>a?b:c</code> 也是不可重载。</p>
|
||
</blockquote>
|
||
<h3 id="-">比较运算符</h3>
|
||
<p>Swift无所知道自定义类型是否相等或不等,因为等于或者不等于由你的代码说了算了。所以自定义的类和结构要使用比较符<code>==</code>或<code>!=</code>就需要重载。</p>
|
||
<p>定义相等运算符函数跟定义其他中置运算符雷同:</p>
|
||
<pre><code class="lang-swift">@infix func == (left: Vector2D, right: Vector2D) -> Bool {
|
||
return (left.x == right.x) && (left.y == right.y)
|
||
}
|
||
|
||
@infix func != (left: Vector2D, right: Vector2D) -> Bool {
|
||
return !(left == right)
|
||
}
|
||
</code></pre>
|
||
<p>上述代码实现了相等运算符<code>==</code>来判断两个<code>Vector2D</code>对象是否有相等的值,相等的概念就是他们有相同的<code>x</code>值和相同的<code>y</code>值,我们就用这个逻辑来实现。接着使用<code>==</code>的结果实现了不相等运算符<code>!=</code>。</p>
|
||
<p>现在我们可以使用这两个运算符来判断两个<code>Vector2D</code>对象是否相等。</p>
|
||
<pre><code class="lang-swift">let twoThree = Vector2D(x: 2.0, y: 3.0)
|
||
let anotherTwoThree = Vector2D(x: 2.0, y: 3.0)
|
||
if twoThree == anotherTwoThree {
|
||
println("这两个向量是相等的.")
|
||
}
|
||
// prints "这两个向量是相等的."
|
||
</code></pre>
|
||
<h3 id="-">自定义运算符</h3>
|
||
<p>标准的运算符不够玩,那你可以声明一些个性的运算符,但个性的运算符只能使用这些字符 <code>/ = - + * % < >!& | ^。~</code>。</p>
|
||
<p>新的运算符声明需在全局域使用<code>operator</code>关键字声明,可以声明为前置,中置或后置的。</p>
|
||
<pre><code class="lang-swift">operator prefix +++ {}
|
||
</code></pre>
|
||
<p>这段代码定义了一个新的前置运算符叫<code>+++</code>,此前Swift并不存在这个运算符。此处为了演示,我们让<code>+++</code>对<code>Vector2D</code>对象的操作定义为 <code>双自增</code> 这样一个独有的操作,这个操作使用了之前定义的加赋运算实现了自已加上自己然后返回的运算。</p>
|
||
<pre><code class="lang-swift">@prefix @assignment func +++ (inout vector: Vector2D) -> Vector2D {
|
||
vector += vector
|
||
return vector
|
||
}
|
||
</code></pre>
|
||
<p><code>Vector2D</code> 的 <code>+++</code> 的实现和 <code>++</code> 的实现很接近, 唯一不同的前者是加自己, 后者是加值为 <code>(1.0, 1.0)</code> 的向量.</p>
|
||
<pre><code class="lang-swift">var toBeDoubled = Vector2D(x: 1.0, y: 4.0)
|
||
let afterDoubling = +++toBeDoubled
|
||
// toBeDoubled 现在是 (2.0, 8.0)
|
||
// afterDoubling 现在也是 (2.0, 8.0)
|
||
</code></pre>
|
||
<h3 id="-">自定义中置运算符的优先级和结合性</h3>
|
||
<p>可以为自定义的中置运算符指定优先级和结合性。可以回头看看<a href="#PrecedenceandAssociativity">优先级和结合性</a>解释这两个因素是如何影响多种中置运算符混合的表达式的计算的。</p>
|
||
<p>结合性(associativity)的值可取的值有<code>left</code>,<code>right</code>和<code>none</code>。左结合运算符跟其他优先级相同的左结合运算符写在一起时,会跟左边的操作数结合。同理,右结合运算符会跟右边的操作数结合。而非结合运算符不能跟其他相同优先级的运算符写在一起。</p>
|
||
<p>结合性(associativity)的值默认为<code>none</code>,优先级(precedence)默认为<code>100</code>。</p>
|
||
<p>以下例子定义了一个新的中置符<code>+-</code>,是左结合的<code>left</code>,优先级为<code>140</code>。</p>
|
||
<pre><code class="lang-swift">operator infix +- { associativity left precedence 140 }
|
||
func +- (left: Vector2D, right: Vector2D) -> Vector2D {
|
||
return Vector2D(x: left.x + right.x, y: left.y - right.y)
|
||
}
|
||
let firstVector = Vector2D(x: 1.0, y: 2.0)
|
||
let secondVector = Vector2D(x: 3.0, y: 4.0)
|
||
let plusMinusVector = firstVector +- secondVector
|
||
// plusMinusVector 此时的值为 (4.0, -2.0)
|
||
</code></pre>
|
||
<p>这个运算符把两个向量的<code>x</code>相加,把向量的<code>y</code>相减。因为他实际是属于加减运算,所以让它保持了和加法一样的结合性和优先级(<code>left</code>和<code>140</code>)。查阅完整的Swift默认结合性和优先级的设置,请移步<a href="../chapter3/04_Expressions.html">表达式</a>;</p>
|
||
|
||
|
||
</section>
|
||
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
|
||
<a href="../chapter2/22_Generics.html" class="navigation navigation-prev " aria-label="Previous page: 泛型"><i class="fa fa-angle-left"></i></a>
|
||
|
||
|
||
<a href="../chapter3/chapter3.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://yandex.st/highlightjs/8.0/styles/default.min.css"></script><script src="http://yandex.st/highlightjs/8.0/highlight.min.js"></script><script text="javascript">$('pre code').each(function(i, block) {hljs.highlightBlock(block);});</script></body>
|
||
|
||
</html>
|