1204 lines
55 KiB
HTML
1204 lines
55 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/10_Statements.html" />
|
||
|
||
|
||
<link rel="prev" href="../chapter3/03_Types.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="3.4" data-basepath=".." data-revision="1402556298428">
|
||
<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>
|
||
</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: 86.84210526315789%;min-width: 84.21052631578948%;"></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 done " data-progress="2.3" style="left: 55.26315789473684%;"></a>
|
||
|
||
<a href="../chapter2/04_Collection_Types.html" title="集合类型" class="chapter done " data-progress="2.4" style="left: 57.89473684210526%;"></a>
|
||
|
||
<a href="../chapter2/05_Control_Flow.html" title="控制流" class="chapter done " data-progress="2.5" style="left: 60.526315789473685%;"></a>
|
||
|
||
<a href="../chapter2/06_Functions.html" title="函数" class="chapter done " data-progress="2.6" style="left: 63.1578947368421%;"></a>
|
||
|
||
<a href="../chapter2/07_Closures.html" title="闭包" class="chapter done " data-progress="2.7" style="left: 65.78947368421052%;"></a>
|
||
|
||
<a href="../chapter2/08_Enumerations.html" title="枚举" class="chapter done " data-progress="2.8" style="left: 68.42105263157895%;"></a>
|
||
|
||
<a href="../chapter2/09_Classes_and_Structures.html" title="类和结构体" class="chapter done " data-progress="2.9" style="left: 71.05263157894737%;"></a>
|
||
|
||
<a href="../chapter3/chapter3.html" title="语言参考" class="chapter done new-chapter" data-progress="3" style="left: 73.6842105263158%;"></a>
|
||
|
||
<a href="../chapter3/01_About_the_Language_Reference.html" title="关于语言参考" class="chapter done " data-progress="3.1" style="left: 76.3157894736842%;"></a>
|
||
|
||
<a href="../chapter3/09_Summary_of_the_Grammar.html" title="语法总结" class="chapter done " data-progress="3.10" style="left: 78.94736842105263%;"></a>
|
||
|
||
<a href="../chapter3/02_Lexical_Structure.html" title="词法结构" class="chapter done " data-progress="3.2" style="left: 81.57894736842105%;"></a>
|
||
|
||
<a href="../chapter3/03_Types.html" title="类型" class="chapter done " data-progress="3.3" style="left: 84.21052631578948%;"></a>
|
||
|
||
<a href="../chapter3/04_Expressions.html" title="表达式" class="chapter done " 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_173">
|
||
|
||
<blockquote>
|
||
<p>翻译:sg552</p>
|
||
<p>校对:numbbbbb</p>
|
||
</blockquote>
|
||
<h1 id="-expressions-">表达式(Expressions)</h1>
|
||
<hr>
|
||
<p>本页包含内容:</p>
|
||
<ul>
|
||
<li>[前缀表达式(Prefix Expressions)]</li>
|
||
<li>[二元表达式(Binary Expressions)]</li>
|
||
<li>[赋值表达式(Assignment Operator)]</li>
|
||
<li>[三元条件运算符(Ternary Conditional Operator)]</li>
|
||
<li>[类型转换运算符(Type-Casting Operators)]</li>
|
||
<li>[主要表达式(Primary Expressions)]</li>
|
||
<li>[后缀表达式(Postfix Expressions)]</li>
|
||
</ul>
|
||
<p>Swift 中存在四种表达式: 前缀(prefix)表达式,二元(binary)表达式,主要(primary)表达式和后缀(postfix)表达式。表达式可以返回一个值,以及运行某些逻辑(causes a side effect)。</p>
|
||
<p>前缀表达式和二元表达式就是对某些表达式使用各种运算符(operators)。 主要表达式是最短小的表达式,它提供了获取(变量的)值的一种途径。 后缀表达式则允许你建立复杂的表达式,例如配合函数调用和成员访问。 每种表达式都在下面有详细论述~</p>
|
||
<blockquote>
|
||
<p>表达式的语法</p>
|
||
<p><em>expression</em> → <em>prefix-expression</em><em>binary-expressions(</em>opt)
|
||
<em>expression-list</em> → <em>expression</em>| <em>expression</em>,<em>expression-list</em></p>
|
||
</blockquote>
|
||
<h2 id="-prefix-expressions-">前缀表达式(Prefix Expressions)</h2>
|
||
<p>前缀表达式由 前缀符号和表达式组成。(这个前缀符号只能接收一个参数)</p>
|
||
<p>Swift 标准库支持如下的前缀操作符:</p>
|
||
<ul>
|
||
<li>++ 自增1 (increment)</li>
|
||
<li>-- 自减1 (decrement)</li>
|
||
<li>! 逻辑否 (Logical NOT )</li>
|
||
<li>~ 按位否 (Bitwise NOT )</li>
|
||
<li>+ 加(Unary plus)</li>
|
||
<li>- 减(Unary minus)</li>
|
||
</ul>
|
||
<p>对于这些操作符的使用,请参见: Basic Operators and Advanced Operators</p>
|
||
<p>作为对上面标准库运算符的补充,你也可以对 某个函数的参数使用 '&'运算符。 更多信息,请参见: "In-Out parameters".</p>
|
||
<blockquote>
|
||
<p>前缀表达式的语法</p>
|
||
<p><em>prefix-expression</em> → <em>prefix-operator</em> (opt) <em>postfix-expression</em>
|
||
<em>prefix-expression</em> → <em>in-out-expression</em>
|
||
<em>in-out-expression</em> → &<em>identifier</em></p>
|
||
</blockquote>
|
||
<h2 id="-binary-expressions-">二元表达式(Binary Expressions)</h2>
|
||
<p>二元表达式由 "左边参数" + "二元运算符" + "右边参数" 组成, 它有如下的形式:</p>
|
||
<p> <code>left-hand argument</code> <code>operator</code> <code>right-hand argument</code></p>
|
||
<p>Swift 标准库提供了如下的二元运算符:</p>
|
||
<ul>
|
||
<li>求幂相关(无结合,优先级160)<ul>
|
||
<li><< 按位左移(Bitwise left shift)</li>
|
||
<li><blockquote>
|
||
<blockquote>
|
||
<p>按位右移(Bitwise right shift)</p>
|
||
</blockquote>
|
||
</blockquote>
|
||
</li>
|
||
</ul>
|
||
</li>
|
||
<li>乘除法相关(左结合,优先级150)<ul>
|
||
<li>* 乘</li>
|
||
<li>/ 除</li>
|
||
<li>% 求余</li>
|
||
<li>&* 乘法,忽略溢出( Multiply, ignoring overflow)</li>
|
||
<li>&/ 除法,忽略溢出(Divide, ignoring overflow)</li>
|
||
<li>&% 求余, 忽略溢出( Remainder, ignoring overflow)</li>
|
||
<li>& 位与( Bitwise AND)</li>
|
||
</ul>
|
||
</li>
|
||
<li>加减法相关(左结合, 优先级140)<ul>
|
||
<li>+ 加</li>
|
||
<li>- 减</li>
|
||
<li>&+ Add with overflow</li>
|
||
<li>&- Subtract with overflow</li>
|
||
<li>| 按位或(Bitwise OR )</li>
|
||
<li>^ 按位异或(Bitwise XOR)</li>
|
||
</ul>
|
||
</li>
|
||
<li>Range (无结合,优先级 135)<ul>
|
||
<li>.. 半闭值域 Half-closed range</li>
|
||
<li>... 全闭值域 Closed range</li>
|
||
</ul>
|
||
</li>
|
||
<li>类型转换 (无结合,优先级 132)<ul>
|
||
<li>is 类型检查( type check)</li>
|
||
<li>as 类型转换( type cast)</li>
|
||
</ul>
|
||
</li>
|
||
<li>Comparative (无结合,优先级 130)<ul>
|
||
<li>< 小于</li>
|
||
<li><= 小于等于</li>
|
||
<li><blockquote>
|
||
<p>大于</p>
|
||
</blockquote>
|
||
</li>
|
||
<li><blockquote>
|
||
<p>= 大于等于</p>
|
||
</blockquote>
|
||
</li>
|
||
<li>== 等于</li>
|
||
<li>!= 不等</li>
|
||
<li>=== 恒等于</li>
|
||
<li>!== 不恒等</li>
|
||
<li>~= 模式匹配( Pattern match)</li>
|
||
</ul>
|
||
</li>
|
||
<li>合取( Conjunctive) (左结合,优先级 120)<ul>
|
||
<li>&& 逻辑与(Logical AND)</li>
|
||
</ul>
|
||
</li>
|
||
<li>析取(Disjunctive) (左结合,优先级 110)<ul>
|
||
<li>|| 逻辑或( Logical OR)</li>
|
||
</ul>
|
||
</li>
|
||
<li>三元条件(Ternary Conditional )(右结合,优先级 100)<ul>
|
||
<li>?: 三元条件 Ternary conditional</li>
|
||
</ul>
|
||
</li>
|
||
<li>赋值 (Assignment) (右结合, 优先级 90)<ul>
|
||
<li>= 赋值(Assign)</li>
|
||
<li>*= Multiply and assign</li>
|
||
<li>/= Divide and assign</li>
|
||
<li>%= Remainder and assign</li>
|
||
<li>+= Add and assign</li>
|
||
<li>-= Subtract and assign</li>
|
||
<li><<= Left bit shift and assign</li>
|
||
<li><blockquote>
|
||
<blockquote>
|
||
<p>= Right bit shift and assign</p>
|
||
</blockquote>
|
||
</blockquote>
|
||
</li>
|
||
<li>&= Bitwise AND and assign</li>
|
||
<li>^= Bitwise XOR and assign</li>
|
||
<li>|= Bitwise OR and assign</li>
|
||
<li>&&= Logical AND and assign</li>
|
||
<li>||= Logical OR and assign</li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<p>关于这些运算符(operators)的更多信息,请参见:Basic Operators and Advanced Operators.</p>
|
||
<blockquote>
|
||
<blockquote>
|
||
<p>注意</p>
|
||
<p>在解析时, 一个二元表达式表示为一个一级数组(a flat list), 这个数组(List)根据运算符的先后顺序,被转换成了一个tree. 例如: 2 + 3 <em> 5 首先被认为是: 2, + , <code>3</code>, </em>, 5. 随后它被转换成 tree (2 + (3 * 5))</p>
|
||
</blockquote>
|
||
<p>二元表达式的语法</p>
|
||
<p><em>binary-expression</em> → <em>binary-operator</em><em>prefix-expression</em>
|
||
<em>binary-expression</em> → <em>assignment-operator</em>prefix-expression<em>
|
||
</em>binary-expression<em> → </em>conditional-operator<em>prefix-expression</em>
|
||
<em>binary-expression</em> → <em>type-casting-operator</em>
|
||
<em>binary-expression</em>s → <em>binary-expression</em><em>binary-expressions</em>(opt)</p>
|
||
</blockquote>
|
||
<h2 id="-assignment-operator-">赋值表达式(Assignment Operator)</h2>
|
||
<p>The assigment operator sets a new value for a given expression. It has the following form:
|
||
赋值表达式会对某个给定的表达式赋值。 它有如下的形式;</p>
|
||
<p><code>expression</code> = <code>value</code></p>
|
||
<p>就是把右边的 <em>value</em> 赋值给左边的 <em>expression</em>. 如果左边的<em>expression</em> 需要接收多个参数(是一个tuple ),那么右边必须也是一个具有同样数量参数的tuple. (允许嵌套的tuple)</p>
|
||
<pre><code class="lang-swift">(a, _, (b, c)) = ("test", 9.45, (12, 3))
|
||
// a is "test", b is 12, c is 3, and 9.45 is ignored
|
||
</code></pre>
|
||
<p>赋值运算符不返回任何值。</p>
|
||
<blockquote>
|
||
<p>赋值表达式的语法</p>
|
||
<p><em>assignment-operator</em> → =</p>
|
||
</blockquote>
|
||
<h2 id="-ternary-conditional-operator-">三元条件运算符(Ternary Conditional Operator)</h2>
|
||
<p>三元条件运算符 是根据条件来获取值。 形式如下:</p>
|
||
<pre><code>`condition` ? `expression used if true` : `expression used if false`
|
||
</code></pre><p>如果 <code>condition</code> 是true, 那么返回 第一个表达式的值(此时不会调用第二个表达式), 否则返回第二个表达式的值(此时不会调用第一个表达式)。</p>
|
||
<p>想看三元条件运算符的例子,请参见: Ternary Conditional Operator.</p>
|
||
<blockquote>
|
||
<p>三元条件表达式</p>
|
||
<p><code>conditional-operator</code> → ?<code>expression</code>:</p>
|
||
</blockquote>
|
||
<h2 id="-type-casting-operators-">类型转换运算符(Type-Casting Operators)</h2>
|
||
<p>有两种类型转换操作符: as 和 is. 它们有如下的形式:</p>
|
||
<pre><code>`expression` as `type`
|
||
`expression` as? `type`
|
||
`expression` is `type`
|
||
</code></pre><p>as 运算符会把<code>目标表达式</code>转换成指定的<code>类型</code>(specified type),过程如下:</p>
|
||
<ul>
|
||
<li><p>如果类型转换成功, 那么目标表达式就会返回指定类型的实例(instance). 例如:把子类(subclass)变成父类(superclass)时.</p>
|
||
</li>
|
||
<li><p>如果转换失败,则会抛出编译错误( compile-time error)。</p>
|
||
</li>
|
||
<li><p>如果上述两个情况都不是(也就是说,编译器在编译时期无法确定转换能否成功,) 那么目标表达式就会变成指定的类型的optional. (is an optional of the specified type ) 然后在运行时,如果转换成功, 目标表达式就会作为 optional的一部分来返回, 否则,目标表达式返回nil. 对应的例子是: 把一个 superclass 转换成一个 subclass.</p>
|
||
</li>
|
||
</ul>
|
||
<pre><code class="lang-swift">class SomeSuperType {}
|
||
class SomeType: SomeSuperType {}
|
||
class SomeChildType: SomeType {}
|
||
let s = SomeType()
|
||
|
||
let x = s as SomeSuperType // known to succeed; type is SomeSuperType
|
||
let y = s as Int // known to fail; compile-time error
|
||
let z = s as SomeChildType // might fail at runtime; type is SomeChildType?
|
||
</code></pre>
|
||
<p>使用'as'做类型转换跟正常的类型声明,对于编译器来说是一样的。例如:</p>
|
||
<pre><code class="lang-swift">let y1 = x as SomeType // Type information from 'as'
|
||
let y2: SomeType = x // Type information from an annotation
|
||
</code></pre>
|
||
<p>'is' 运算符在“运行时(runtime)”会做检查。 成功会返回true, 否则 false</p>
|
||
<p>The check must not be known to be true or false at compile time. The following are invalid:
|
||
上述检查在“编译时(compile time)”不能使用。 例如下面的使用是错误的:</p>
|
||
<pre><code class="lang-swift">"hello" is String
|
||
"hello" is Int
|
||
</code></pre>
|
||
<p>关于类型转换的更多内容和例子,请参见: Type Casting.</p>
|
||
<blockquote>
|
||
<p>类型转换的语法</p>
|
||
<p><em>type-casting-operator</em> → is<em>type</em>| as?(opt)<em>type</em></p>
|
||
</blockquote>
|
||
<h2 id="-primary-expressions-">主要表达式(Primary Expressions)</h2>
|
||
<p><code>主要表达式</code>是最基本的表达式。 它们可以跟 前缀表达式,二元表达式,后缀表达式以及其他主要表达式组合使用。</p>
|
||
<blockquote>
|
||
<p>主要表达式的语法</p>
|
||
<p><em>primary-expression</em> → <em>identifier</em><em>generic-argument-clause</em>(opt)
|
||
<em>primary-expression</em> → <em>literal-expression</em>
|
||
<em>primary-expression</em> → <em>self-expression</em>
|
||
<em>primary-expression</em> → <em>superclass-expression</em>
|
||
<em>primary-expression</em> → <em>closure-expression</em>
|
||
<em>primary-expression</em> → <em>parenthesized-expression</em>
|
||
<em>primary-expression</em> → <em>implicit-member-expression</em>
|
||
<em>primary-expression</em> → <em>wildcard-expression</em></p>
|
||
</blockquote>
|
||
<h3 id="-literal-expression-">字符型表达式(Literal Expression)</h3>
|
||
<p>由这些内容组成:普通的字符(string, number) , 一个字符的字典或者数组,或者下面列表中的特殊字符。</p>
|
||
<table>
|
||
<thead>
|
||
<tr>
|
||
<th>字符(Literal)</th>
|
||
<th>类型(Type)</th>
|
||
<th>值(Value)</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr>
|
||
<td>_<em>FILE_</em></td>
|
||
<td>String</td>
|
||
<td>所在的文件名</td>
|
||
</tr>
|
||
<tr>
|
||
<td>_<em>LINE_</em></td>
|
||
<td>Int</td>
|
||
<td>所在的行数</td>
|
||
</tr>
|
||
<tr>
|
||
<td>_<em>COLUMN_</em></td>
|
||
<td>Int</td>
|
||
<td>所在的列数</td>
|
||
</tr>
|
||
<tr>
|
||
<td>_<em>FUNCTION_</em></td>
|
||
<td>String</td>
|
||
<td>所在的function 的名字</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
<p>在某个函数(function)中,<code>__FUNCTION__</code> 会返回当前函数的名字。 在某个方法(method)中,它会返回当前方法的名字。 在某个property 的getter/setter中会返回这个属性的名字。 在init/subscript中 只有的特殊成员(member)中会返回这个keyword的名字,在某个文件的顶端(the top level of a file),它返回的是当前module的名字。</p>
|
||
<p>一个array literal,是一个有序的值的集合。 它的形式是:</p>
|
||
<pre><code>[`value 1`, `value 2`, `...`]
|
||
</code></pre><p>数组中的最后一个表达式可以紧跟一个逗号(','). []表示空数组 。 array literal的type是 T[], 这个T就是数组中元素的type. 如果该数组中有多种type, T则是跟这些type的公共supertype最接近的type.(closest common supertype)</p>
|
||
<p>一个<code>dictionary literal</code> 是一个包含无序的键值对(key-value pairs)的集合,它的形式是:</p>
|
||
<pre><code>[`key 1`: `value 1`, `key 2`: `value 2`, `...`]
|
||
</code></pre><p>dictionary 的最后一个表达式可以是一个逗号(','). [:] 表示一个空的dictionary. 它的type是 Dictionary<KeyType, ValueType> (这里KeyType表示 key的type, ValueType表示 value的type) 如果这个dictionary 中包含多种 types, 那么KeyType, Value 则对应着它们的公共supertype最接近的type( closest common supertype).</p>
|
||
<blockquote>
|
||
<p>字符型表达式的语法</p>
|
||
<p><em>literal-expression</em> → <em>literal</em>
|
||
<em>literal-expression</em> → <em>array-literal</em>| <em>dictionary-literal</em>
|
||
<em>literal-expression</em> → <em>_<em>FILE_</em></em>| <em>_<em>LINE_</em></em>| <em>_<em>COLUMN_</em></em>| <em>_<em>FUNCTION_</em></em>
|
||
<em>array-literal</em> → [<em>array-literal-items</em>opt]
|
||
<em>array-literal-items</em> → <em>array-literal-item</em>,(opt) | <em>array-literal-item</em>,<em>array-literal-items</em>
|
||
<em>array-literal-item</em> → <em>expression</em>
|
||
<em>dictionary-literal</em> → [<em>dictionary-literal-items</em>] [:]
|
||
<em>dictionary-literal-items</em> → <em>dictionary-literal-item</em>,(opt)| <em>dictionary-literal-item</em>,<em>dictionary-literal-items</em>
|
||
<em>dictionary-literal-item</em> → <em>expression</em>:<em>expression</em></p>
|
||
</blockquote>
|
||
<h3 id="self-self-expression-">self表达式(Self Expression)</h3>
|
||
<p>self表达式是对 当前type 或者当前instance的引用。它的形式如下:</p>
|
||
<blockquote>
|
||
<p>self
|
||
self.<code>member name</code>
|
||
self[<code>subscript index</code>]
|
||
self(<code>initializer arguments</code>)
|
||
self.init(<code>initializer arguments</code>)</p>
|
||
</blockquote>
|
||
<p>如果在 initializer, subscript, instance method中,self等同于当前type的instance. 在一个静态方法(static method), 类方法(class method)中, self等同于当前的type.</p>
|
||
<p>当访问 member(成员变量时), self 用来区分重名变量(例如函数的参数). 例如,
|
||
(下面的 self.greeting 指的是 var greeting: String, 而不是 init(greeting: String) )</p>
|
||
<pre><code class="lang-swift">class SomeClass {
|
||
var greeting: String
|
||
init(greeting: String) {
|
||
self.greeting = greeting
|
||
}
|
||
}
|
||
</code></pre>
|
||
<p>在mutating 方法中, 你可以使用self 对 该instance进行赋值。</p>
|
||
<pre><code class="lang-swift">struct Point {
|
||
var x = 0.0, y = 0.0
|
||
mutating func moveByX(deltaX: Double, y deltaY: Double) {
|
||
self = Point(x: x + deltaX, y: y + deltaY)
|
||
}
|
||
}
|
||
</code></pre>
|
||
<blockquote>
|
||
<p>self表达式的语法</p>
|
||
<p><em>self-expression</em> → self
|
||
<em>self-expression</em> → self.<em>identifier</em>
|
||
<em>self-expression</em> → self[<em>expression</em>]
|
||
<em>self-expression</em> → self.init</p>
|
||
</blockquote>
|
||
<h3 id="-superclass-expression-">超类表达式(Superclass Expression)</h3>
|
||
<p>超类表达式可以使我们在某个class中访问它的超类. 它有如下形式:</p>
|
||
<pre><code>super.`member name`
|
||
super[`subscript index`]
|
||
super.init(`initializer arguments`)
|
||
</code></pre><p>形式1 用来访问超类的某个成员(member). 形式2 用来访问该超类的 subscript 实现。 形式3 用来访问该超类的 initializer.</p>
|
||
<p>子类(subclass)可以通过超类(superclass)表达式在它们的 member, subscripting 和 initializers 中来利用它们超类中的某些实现(既有的方法或者逻辑)。</p>
|
||
<blockquote>
|
||
<p>GRAMMAR OF A SUPERCLASS EXPRESSION</p>
|
||
<p><em>superclass-expression</em> → <em>superclass-method-expression</em> | <em>superclass-subscript-expression</em>| <em>superclass-initializer-expression</em>
|
||
<em>superclass-method-expression</em> → super.<em>identifier</em>
|
||
<em>superclass-subscript-expression</em> → super[<em>expression</em>]
|
||
<em>superclass-initializer-expression</em> → super.init</p>
|
||
</blockquote>
|
||
<h3 id="-closure-expression-">闭包表达式(Closure Expression)</h3>
|
||
<p>闭包(closure) 表达式可以建立一个闭包(在其他语言中也叫 lambda, 或者 匿名函数(anonymous function)). 跟函数(function)的声明一样, 闭包(closure)包含了可执行的代码(跟方法主体(statement)类似) 以及接收(capture)的参数。 它的形式如下:</p>
|
||
<pre><code class="lang-swift"> { (parameters) -> return type in
|
||
statements
|
||
}
|
||
</code></pre>
|
||
<p>闭包的参数声明形式跟方法中的声明一样, 请参见:Function Declaration.</p>
|
||
<p>闭包还有几种特殊的形式, 让使用更加简洁:</p>
|
||
<ul>
|
||
<li>闭包可以省略 它的参数的type 和返回值的type. 如果省略了参数和参数类型,就也要省略 'in'关键字。 如果被省略的type 无法被编译器获知(inferred) ,那么就会抛出编译错误。</li>
|
||
<li>闭包可以省略参数,转而在方法体(statement)中使用 $0, $1, $2 来引用出现的第一个,第二个,第三个参数。</li>
|
||
<li>如果闭包中只包含了一个表达式,那么该表达式就会自动成为该闭包的返回值。 在执行 'type inference '时,该表达式也会返回。</li>
|
||
</ul>
|
||
<p>下面几个 闭包表达式是 等价的:</p>
|
||
<pre><code class="lang-swift">myFunction {
|
||
(x: Int, y: Int) -> Int in
|
||
return x + y
|
||
}
|
||
|
||
myFunction {
|
||
(x, y) in
|
||
return x + y
|
||
}
|
||
|
||
myFunction { return $0 + $1 }
|
||
|
||
myFunction { $0 + $1 }
|
||
</code></pre>
|
||
<p>关于 向闭包中传递参数的内容,参见: Function Call Expression.</p>
|
||
<p>闭包表达式可以通过一个参数列表(capture list) 来显式指定它需要的参数。 参数列表 由中括号 [] 括起来,里面的参数由逗号','分隔。一旦使用了参数列表,就必须使用'in'关键字(在任何情况下都得这样做,包括忽略参数的名字,type, 返回值时等等)。</p>
|
||
<p>在闭包的参数列表( capture list)中, 参数可以声明为 'weak' 或者 'unowned' .</p>
|
||
<pre><code class="lang-swift">myFunction { print(self.title) } // strong capture
|
||
myFunction { [weak self] in print(self!.title) } // weak capture
|
||
myFunction { [unowned self] in print(self.title) } // unowned capture
|
||
</code></pre>
|
||
<p>在参数列表中,也可以使用任意表达式来赋值. 该表达式会在 闭包被执行时赋值,然后按照不同的力度来获取(这句话请慎重理解)。(captured with the specified strength. ) 例如:</p>
|
||
<pre><code class="lang-swift">// Weak capture of "self.parent" as "parent"
|
||
myFunction { [weak parent = self.parent] in print(parent!.title) }
|
||
</code></pre>
|
||
<p>关于闭包表达式的更多信息和例子,请参见: Closure Expressions.</p>
|
||
<blockquote>
|
||
<p>闭包表达式的语法</p>
|
||
<p><em>closure-expression</em> → {<em>closure-signature</em>opt<em>statements</em>}
|
||
<em>closure-signature</em> → <em>parameter-clause</em><em>function-result</em>(opt)in
|
||
<em>closure-signature</em> → <em>identifier-list</em><em>function-result</em>(opt)in
|
||
<em>closure-signature</em> → <em>capture-list</em><em>parameter-clause</em><em>function-result</em>(opt)in
|
||
<em>closure-signature</em> → <em>capture-list</em><em>identifier-list</em><em>function-result</em>(opt)in
|
||
<em>closure-signature</em> → <em>capture-list</em>in
|
||
<em>capture-list</em> → [<em>capture-specifier</em><em>expression</em>]
|
||
<em>capture-specifier</em> → weak| unowned| unowned(safe)| unowned(unsafe)</p>
|
||
</blockquote>
|
||
<h3 id="-implicit-member-expression-">隐式成员表达式(Implicit Member Expression)</h3>
|
||
<p>在可以判断出类型(type)的上下文(context)中,隐式成员表达式是访问某个type的member( 例如 class method, enumeration case) 的简洁方法。 它的形式是:</p>
|
||
<p>.<code>member name</code></p>
|
||
<p>例子:</p>
|
||
<pre><code class="lang-swift">var x = MyEnumeration.SomeValue
|
||
x = .AnotherValue
|
||
</code></pre>
|
||
<blockquote>
|
||
<p> 隐式成员表达式的语法</p>
|
||
<p> <em>implicit-member-expression</em> → .<em>identifier</em></p>
|
||
</blockquote>
|
||
<h3 id="-parenthesized-expression-">圆括号表达式(Parenthesized Expression)</h3>
|
||
<p>圆括号表达式由多个子表达式和逗号','组成。 每个子表达式前面可以有 identifier x: 这样的可选前缀。形式如下:</p>
|
||
<p>(<code>identifier 1</code>: <code>expression 1</code>, <code>identifier 2</code>: <code>expression 2</code>, <code>...</code>)</p>
|
||
<p>圆括号表达式用来建立tuples , 然后把它做为参数传递给 function. 如果某个圆括号表达式中只有一个 子表达式,那么它的type就是 子表达式的type。例如: (1)的 type是Int, 而不是(Int)</p>
|
||
<blockquote>
|
||
<p>圆括号表达式的语法</p>
|
||
<p><em>parenthesized-expression</em> → (<em>expression-element-list</em> (opt))
|
||
<em>expression-element-list</em> → <em>expression-element</em>| <em>expression-element</em>,<em>expression-element-list</em>
|
||
<em>expression-element</em> → <em>expression</em>| <em>identifier</em>:<em>expression</em></p>
|
||
</blockquote>
|
||
<h3 id="-wildcard-expression-">通配符表达式(Wildcard Expression)</h3>
|
||
<p>通配符表达式用来忽略传递进来的某个参数。例如:下面的代码中,10被传递给x, 20被忽略(译注:好奇葩的语法。。。)</p>
|
||
<pre><code class="lang-swift">(x, _) = (10, 20)
|
||
// x is 10, 20 is ignored
|
||
</code></pre>
|
||
<blockquote>
|
||
<p>通配符表达式的语法</p>
|
||
<p><em>wildcard-expression</em> → _</p>
|
||
</blockquote>
|
||
<h2 id="-postfix-expressions-">后缀表达式(Postfix Expressions)</h2>
|
||
<p>后缀表达式就是在某个表达式的后面加上 操作符。 严格的讲,每个主要表达式(primary expression)都是一个后缀表达式</p>
|
||
<p>Swift 标准库提供了下列后缀表达式:</p>
|
||
<ul>
|
||
<li>++ Increment</li>
|
||
<li>-- Decrement</li>
|
||
</ul>
|
||
<p>对于这些操作符的使用,请参见: Basic Operators and Advanced Operators</p>
|
||
<blockquote>
|
||
<p>后缀表达式的语法</p>
|
||
<p><em>postfix-expression</em> → <em>primary-expression</em>
|
||
<em>postfix-expression</em> → <em>postfix-expression</em><em>postfix-operator</em>
|
||
<em>postfix-expression</em> → <em>function-call-expression</em>
|
||
<em>postfix-expression</em> → <em>initializer-expression</em>
|
||
<em>postfix-expression</em> → <em>explicit-member-expression</em>
|
||
<em>postfix-expression</em> → <em>postfix-self-expression</em>
|
||
<em>postfix-expression</em> → <em>dynamic-type-expression</em>
|
||
<em>postfix-expression</em> → <em>subscript-expression</em>
|
||
<em>postfix-expression</em> → <em>forced-value-expression</em>
|
||
<em>postfix-expression</em> → <em>optional-chaining-expression</em></p>
|
||
</blockquote>
|
||
<h3 id="-function-call-expression-">函数调用表达式(Function Call Expression)</h3>
|
||
<p>函数调用表达式由函数名和参数列表组成。它的形式如下:</p>
|
||
<p><code>function name</code>(<code>argument value 1</code>, <code>argument value 2</code>)</p>
|
||
<p>The function name can be any expression whose value is of a function type.
|
||
(不用翻译了, 太罗嗦)</p>
|
||
<p>如果该function 的声明中指定了参数的名字,那么在调用的时候也必须得写出来. 例如:</p>
|
||
<p><code>function name</code>(<code>argument name 1</code>: <code>argument value 1</code>, <code>argument name 2</code>: <code>argument value 2</code>)</p>
|
||
<p>可以在 函数调用表达式的尾部(最后一个参数之后)加上 一个闭包(closure) , 该闭包会被目标函数理解并执行。它具有如下两种写法:</p>
|
||
<pre><code class="lang-swift">// someFunction takes an integer and a closure as its arguments
|
||
someFunction(x, {$0 == 13})
|
||
someFunction(x) {$0 == 13}
|
||
</code></pre>
|
||
<p>如果闭包是该函数的唯一参数,那么圆括号可以省略。</p>
|
||
<pre><code class="lang-swift">// someFunction takes a closure as its only argument
|
||
myData.someMethod() {$0 == 13}
|
||
myData.someMethod {$0 == 13}
|
||
</code></pre>
|
||
<blockquote>
|
||
<p>GRAMMAR OF A FUNCTION CALL EXPRESSION</p>
|
||
<p><em>function-call-expression</em> → <em>postfix-expression</em><em>parenthesized-expression</em>
|
||
<em>function-call-expression</em> → <em>postfix-expression</em><em>parenthesized-expression</em>(opt)<em>trailing-closure</em>
|
||
<em>trailing-closure</em> → <em>closure-expression</em></p>
|
||
</blockquote>
|
||
<h3 id="-initializer-expression-">初始化函数表达式(Initializer Expression)</h3>
|
||
<p>Initializer表达式用来给某个Type初始化。 它的形式如下:</p>
|
||
<p><code>expression</code>.init(<code>initializer arguments</code>)</p>
|
||
<p>(Initializer表达式用来给某个Type初始化。) 跟函数(function)不同, initializer 不能返回值。</p>
|
||
<pre><code class="lang-swift">var x = SomeClass.someClassFunction // ok
|
||
var y = SomeClass.init // error
|
||
```swift
|
||
|
||
可以通过 initializer 表达式来委托调用(delegate to )到superclass的initializers.
|
||
|
||
```swift
|
||
class SomeSubClass: SomeSuperClass {
|
||
init() {
|
||
// subclass initialization goes here
|
||
super.init()
|
||
}
|
||
}
|
||
</code></pre>
|
||
<blockquote>
|
||
<p>initializer表达式的语法</p>
|
||
<p><em>initializer-expression</em> → <em>postfix-expression</em>.init</p>
|
||
</blockquote>
|
||
<h3 id="-explicit-member-expression-">显式成员表达式(Explicit Member Expression)</h3>
|
||
<p>显示成员表达式允许我们访问type, tuple, module的成员变量。它的形式如下:</p>
|
||
<p><code>expression</code>.<code>member name</code></p>
|
||
<p>该member 就是某个type在声明时候所定义(declaration or extension) 的变量, 例如:</p>
|
||
<pre><code class="lang-swift">class SomeClass {
|
||
var someProperty = 42
|
||
}
|
||
let c = SomeClass()
|
||
let y = c.someProperty // Member access
|
||
</code></pre>
|
||
<p>对于tuple, 要根据它们出现的顺序(0, 1, 2...)来使用:</p>
|
||
<pre><code class="lang-swift">var t = (10, 20, 30)
|
||
t.0 = t.1
|
||
// Now t is (20, 20, 30)
|
||
</code></pre>
|
||
<p>The members of a module access the top-level declarations of that module.
|
||
(不确定:对于某个module的member的调用,只能调用在top-level声明中的member.)</p>
|
||
<blockquote>
|
||
<p>显示成员表达式的语法</p>
|
||
<p><em>explicit-member-expression</em> → <em>postfix-expression</em>.<em>decimal-digit</em>
|
||
<em>explicit-member-expression</em> → <em>postfix-expression</em>.<em>identifier</em><em>generic-argument-clause</em>(opt)</p>
|
||
</blockquote>
|
||
<h3 id="-self-postfix-self-expression-">后缀self表达式(Postfix Self Expression)</h3>
|
||
<p>后缀表达式由 某个表达式 + '.self' 组成. 形式如下:</p>
|
||
<p><code>expression</code>.self
|
||
<code>type</code>.self</p>
|
||
<p>形式1 表示会返回 expression 的值。例如: x.self 返回 x</p>
|
||
<p>形式2:返回对应的type。我们可以用它来动态的获取某个instance的type。</p>
|
||
<blockquote>
|
||
<p>后缀self表达式的语法</p>
|
||
<p><em>postfix-self-expression</em> → <em>postfix-expression</em>.self</p>
|
||
</blockquote>
|
||
<h3 id="dynamic-dynamic-type-expression-">dynamic表达式(Dynamic Type Expression)</h3>
|
||
<p>(因为dynamicType是一个独有的方法,所以这里保留了英文单词,未作翻译, --- 类似与self expression)</p>
|
||
<p>dynamicType 表达式由 某个表达式 + '.dynamicType' 组成。</p>
|
||
<p><code>expression</code>.dynamicType</p>
|
||
<p>上面的形式中, expression 不能是某type的名字(当然了,如果我都知道它的名字了还需要动态来获取它吗)。动态类型表达式会返回"运行时"某个instance的type, 具体请看下面的列子:</p>
|
||
<pre><code class="lang-swift">class SomeBaseClass {
|
||
class func printClassName() {
|
||
println("SomeBaseClass")
|
||
}
|
||
}
|
||
class SomeSubClass: SomeBaseClass {
|
||
override class func printClassName() {
|
||
println("SomeSubClass")
|
||
}
|
||
}
|
||
let someInstance: SomeBaseClass = SomeSubClass()
|
||
|
||
// someInstance is of type SomeBaseClass at compile time, but
|
||
// someInstance is of type SomeSubClass at runtime
|
||
someInstance.dynamicType.printClassName()
|
||
// prints "SomeSubClass"
|
||
</code></pre>
|
||
<blockquote>
|
||
<p>dynamic type 表达式</p>
|
||
<p><em>dynamic-type-expression</em> → <em>postfix-expression</em>.dynamicType</p>
|
||
</blockquote>
|
||
<h3 id="-subscript-expression-">附属脚本表达式(Subscript Expression)</h3>
|
||
<p>附属脚本表达式提供了通过附属脚本访问getter/setter 的方法。它的形式是:</p>
|
||
<p><code>expression</code>[<code>index expressions</code>]</p>
|
||
<p>可以通过附属脚本表达式通过getter获取某个值,或者通过setter赋予某个值.</p>
|
||
<p>关于subscript的声明,请参见: Protocol Subscript Declaration.</p>
|
||
<blockquote>
|
||
<p>附属脚本表达式的语法</p>
|
||
<p><em>subscript-expression</em> → <em>postfix-expression</em>[<em>expression-list</em>]</p>
|
||
</blockquote>
|
||
<h3 id="-forced-value-expression-">强制取值表达式(Forced-Value Expression)</h3>
|
||
<p>强制取值表达式用来获取某个目标表达式的值(该目标表达式的值必须不是nil )。它的形式如下:</p>
|
||
<p><code>expression</code>!</p>
|
||
<p>如果该表达式的值不是nil, 则返回对应的值。 否则,抛出运行时错误(runtime error)。</p>
|
||
<blockquote>
|
||
<p>强制取值表达式的语法</p>
|
||
<p><em>forced-value-expression</em> → <em>postfix-expression</em>!</p>
|
||
</blockquote>
|
||
<h3 id="-optional-chaining-expression-">可选链表达式(Optional-Chaining Expression)</h3>
|
||
<p>可选链表达式由目标表达式 + '?' 组成,形式如下:</p>
|
||
<p><code>expression</code>?</p>
|
||
<p>后缀'?' 返回目标表达式的值,把它做为可选的参数传递给后续的表达式</p>
|
||
<p>如果某个后缀表达式包含了可选链表达式,那么它的执行过程就比较特殊: 首先先判断该可选链表达式的值,如果是 nil, 整个后缀表达式都返回 nil, 如果该可选链的值不是nil, 则正常返回该后缀表达式的值(依次执行它的各个子表达式)。在这两种情况下,该后缀表达式仍然是一个optional type(In either case, the value of the postfix expression is still of an optional type)</p>
|
||
<p>如果某个"后缀表达式"的"子表达式"中包含了"可选链表达式",那么只有最外层的表达式返回的才是一个optional type. 例如,在下面的例子中, 如果c 不是nil, 那么 c?.property.performAction() 这句代码在执行时,就会先获得c 的property方法,然后调用 performAction()方法。 然后对于 "c?.property.performAction()" 这个整体,它的返回值是一个optional type.</p>
|
||
<pre><code class="lang-swift">var c: SomeClass?
|
||
var result: Bool? = c?.property.performAction()
|
||
</code></pre>
|
||
<p>如果不使用可选链表达式,那么 上面例子的代码跟下面例子等价:</p>
|
||
<pre><code class="lang-swift">if let unwrappedC = c {
|
||
result = unwrappedC.property.performAction()
|
||
}
|
||
</code></pre>
|
||
<blockquote>
|
||
<p>可选链表达式的语法</p>
|
||
<p><em>optional-chaining-expression</em> → <em>postfix-expression</em>?</p>
|
||
</blockquote>
|
||
|
||
|
||
</section>
|
||
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
|
||
<a href="../chapter3/03_Types.html" class="navigation navigation-prev " aria-label="Previous page: 类型"><i class="fa fa-angle-left"></i></a>
|
||
|
||
|
||
<a href="../chapter3/10_Statements.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>
|