919 lines
51 KiB
HTML
919 lines
51 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="../chapter2/10_Properties.html" />
|
||
|
||
|
||
<link rel="prev" href="../chapter2/08_Enumerations.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.9" 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: 71.05263157894737%;min-width: 68.42105263157895%;"></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 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_219">
|
||
|
||
<blockquote>
|
||
<p>翻译:JaySurplus</p>
|
||
<p>校对:sg552</p>
|
||
</blockquote>
|
||
<h1 id="-">类和结构体</h1>
|
||
<p>本页包含内容:</p>
|
||
<ul>
|
||
<li><a href="#comparing_classes_and_structures">类和结构体对比</a></li>
|
||
<li><a href="#structures_and_enumerations_are_value_types">结构体和枚举是值类型</a></li>
|
||
<li><a href="#classes_are_reference_types">类是引用类型</a></li>
|
||
<li><a href="#choosing_between_classes_and_structures">类和结构体的选择</a></li>
|
||
<li><a href="#assignment_and_copy_behavior_for_collection_types">集合(collection)类型的赋值与复制行为</a></li>
|
||
</ul>
|
||
<p>类和结构体是人们构建代码所用的一种通用且灵活的构造体。为了在类和结构体中实现各种功能,我们必须要严格按照对于常量,变量以及函数所规定的语法规则来定义属性和添加方法。</p>
|
||
<p>与其他编程语言所不同的是,Swift 并不要求你为自定义类和结构去创建独立的接口和实现文件。你所要做的是在一个单一文件中定义一个类或者结构体,系统将会自动生成面向其它代码的外部接口。</p>
|
||
<blockquote>
|
||
<p> 注意:</p>
|
||
<p>通常一个<code>类</code>的实例被称为<code>对象</code>。然而在Swift 中,类和结构体的关系要比在其他语言中更加的密切,本章中所讨论的大部分功能都可以用在类和结构体上。因此,我们会主要使用<code>实例</code>而不是<code>对象</code>。</p>
|
||
</blockquote>
|
||
<p><a name="comparing_classes_and_structures"></a></p>
|
||
<h3 id="-">类和结构体对比</h3>
|
||
<p>Swift 中类和结构体有很多共同点。共同处在于:</p>
|
||
<ul>
|
||
<li>定义属性用于储存值</li>
|
||
<li>定义方法用于提供功能</li>
|
||
<li>定义附属脚本用于访问值</li>
|
||
<li>定义构造器用于生成初始化值</li>
|
||
<li>通过扩展以增加默认实现的功能</li>
|
||
<li>符合协议以对某类提供标准功能</li>
|
||
</ul>
|
||
<p>更多信息请参见 <a href="10_Properties.html">属性</a>,<a href="11_Methods.html">方法</a>,<a href="12_Subscripts.html">附属脚本</a>,<a href="14_Initialization.html">初始过程</a>,<a href="20_Extensions.html">扩展</a>,和<a href="21_Protocols.html">协议</a>。</p>
|
||
<p>与结构体相比,类还有如下的附加功能:</p>
|
||
<ul>
|
||
<li>继承允许一个类继承另一个类的特征</li>
|
||
<li>类型转换允许在运行时检查和解释一个类实例的类型</li>
|
||
<li>解构器允许一个类实例释放任何其所被分配的资源</li>
|
||
<li>引用计数允许对一个类的多次引用</li>
|
||
</ul>
|
||
<p>更多信息请参见<a href="http://" target="_blank">继承</a>,<a href="http://" target="_blank">类型转换</a>,<a href="http://" target="_blank">初始化</a>,和<a href="http://" target="_blank">自动引用计数</a>。</p>
|
||
<blockquote>
|
||
<p>注意:</p>
|
||
<p>结构体总是通过被复制的方式在代码中传递,因此请不要使用引用计数。</p>
|
||
</blockquote>
|
||
<h3 id="-">定义</h3>
|
||
<p>类和结构体有着类似的定义方式。我们通过关键字<code>class</code>和<code>struct</code>来分别表示类和结构体,并在一对大括号中定义它们的具体内容:</p>
|
||
<pre><code>class SomeClass {
|
||
// class definition goes here
|
||
}
|
||
struct SomeStructure {
|
||
// structure definition goes here
|
||
}
|
||
</code></pre><blockquote>
|
||
<p> 注意:</p>
|
||
<p>在你每次定义一个新类或者结构体的时候,实际上你是有效地定义了一个新的 Swift 类型。因此请使用 <code>UpperCamelCase</code> 这种方式来命名(如 <code>SomeClass</code> 和<code>SomeStructure</code>等),以便符合标准Swift 类型的大写命名风格(如<code>String</code>,<code>Int</code>和<code>Bool</code>)。相反的,请使用<code>lowerCamelCase</code>这种方式为属性和方法命名(如<code>framerate</code>和<code>incrementCount</code>),以便和类区分。</p>
|
||
</blockquote>
|
||
<p>以下是定义结构体和定义类的示例:</p>
|
||
<pre><code>struct Resolution {
|
||
var width = 0
|
||
var heigth = 0
|
||
}
|
||
class VideoMode {
|
||
var resolution = Resolution()
|
||
var interlaced = false
|
||
var frameRate = 0.0
|
||
var name: String?
|
||
}
|
||
</code></pre><p>在上面的示例中我们定义了一个名为<code>Resolution</code>的结构体,用来描述一个显示器的像素分辨率。这个结构体包含了两个名为<code>width</code>和<code>height</code>的储存属性。储存属性是捆绑和储存在类或结构体中的常量或变量。当这两个属性被初始化为整数<code>0</code>的时候,它们会被推断为<code>Int</code>类型。</p>
|
||
<p>在上面的示例中我们还定义了一个名为<code>VideoMode</code>的类,用来描述一个视频显示器的特定模式。这个类包含了四个储存属性变量。第一个是<code>分辨率</code>,它被初始化为一个新的<code>Resolution</code>结构体的实例,具有<code>Resolution</code>的属性类型。新<code>VideoMode</code>实例同时还会初始化其它三个属性,它们分别是,初始值为<code>false</code>(意为“non-interlaced video”)的<code>inteflaced</code>,回放帧率初始值为<code>0.0</code>的<code>frameRate</code>和值为可选<code>String</code>的<code>name</code>。<code>name</code>属性会被自动赋予一个默认值<code>nil</code>,意为“没有<code>name</code>值”,因它是一个可选类型。</p>
|
||
<h3 id="-">类和结构体实例</h3>
|
||
<p><code>Resolution</code>结构体和<code>VideoMode</code>类的定义仅描述了什么是<code>Resolution</code>和<code>VideoMode</code>。它们并没有描述一个特定的分辨率(resolution)或者视频模式(video mode)。为了描述一个特定的分辨率或者视频模式,我们需要生成一个它们的实例。</p>
|
||
<p>生成结构体和类实例的语法非常相似:</p>
|
||
<pre><code>let someResolution = Resolution()
|
||
let someVideoMode = VideoMode()
|
||
</code></pre><p>结构体和类都使用构造器语法来生成新的实例。构造器语法的最简单形式是在结构体或者类的类型名称后跟随一个空括弧,如<code>Resolution()</code>或<code>VideoMode()</code>。通过这种方式所创建的类或者结构体实例,其属均会被初始化为默认值。<a href="14_Initialization.html">构造过程</a>章节会对类和结构体的初始化进行更详细的讨论。</p>
|
||
<h3 id="-">属性访问</h3>
|
||
<p>通过使用<em>点语法</em>(<em>dot syntax</em>),你可以访问实例中所含有的属性。其语法规则是,实例名后面紧跟属性名,两者通过点号(.)连接:</p>
|
||
<pre><code>println("The width of someResolution is \(someResolution.width)")
|
||
// 输出 "The width of someResolution is 0"
|
||
</code></pre><p>在上面的例子中,<code>someResolution.width</code>引用<code>someResolution</code>的<code>width</code>属性,返回<code>width</code>的初始值<code>0</code>。</p>
|
||
<p>你也可以访问子属性,如何<code>VideoMode</code>中<code>Resolution</code>属性的<code>width</code>属性:</p>
|
||
<pre><code>println("The width of someVideoMode is \(someVideoMode.resolution.width)")
|
||
// 输出 "The width of someVideoMode is 0"
|
||
</code></pre><p>你也可以使用点语法为属性变量赋值:</p>
|
||
<pre><code>someVideoMode.resolution.width = 12880
|
||
println("The width of someVideoMode is now \(someVideoMode.resolution.width)")
|
||
// 输出 "The width of someVideoMode is now 1280"
|
||
</code></pre><blockquote>
|
||
<p> 注意:</p>
|
||
<p>与 Objective-C 语言不同的是,Swift 允许直接设置结构体属性的子属性。上面的最后一个例子,就是直接设置了<code>someVideoMode</code>中<code>resolution</code>属性的<code>width</code>这个子属性,以上操作并不需要从新设置<code>resolution</code>属性。</p>
|
||
</blockquote>
|
||
<h3 id="-">结构体类型的成员逐一构造器</h3>
|
||
<p>//Memberwise Initializers for structure Types</p>
|
||
<p>所有结构体都有一个自动生成的成员逐一构造器,用于初始化新结构体实例中成员的属性。新实例中各个属性的初始值可以通过属性的名称传递到成员逐一构造器之中:</p>
|
||
<pre><code>let vga = resolution(width:640, heigth: 480)
|
||
</code></pre><p>与结构体不同,类实例没有默认的成员逐一构造器。<a href="14_Initialization.html">构造过程</a>章节会对构造器进行更详细的讨论。</p>
|
||
<p><a name="structures_and_enumerations_are_value_types"></a></p>
|
||
<h2 id="-">结构体和枚举是值类型</h2>
|
||
<p>值类型被赋予给一个变量,常数或者本身被传递给一个函数的时候,实际上操作的是其的拷贝。</p>
|
||
<p>在之前的章节中,我们已经大量使用了值类型。实际上,在 Swift 中,所有的基本类型:整数(Integer)、浮点数(floating-point)、布尔值(Booleans)、字符串(string)、数组(array)和字典(dictionaries),都是值类型,并且都是以结构体的形式在后台所实现。</p>
|
||
<p>在 Swift 中,所有的结构体和枚举都是值类型。这意味着它们的实例,以及实例中所包含的任何值类型属性,在代码中传递的时候都会被复制。</p>
|
||
<p>请看下面这个示例,其使用了前一个示例中<code>Resolution</code>结构体:</p>
|
||
<pre><code>let hd = Resolution(width: 1920, height: 1080)
|
||
var cinema = hd
|
||
</code></pre><p>在以上示例中,声明了一个名为<code>hd</code>的常量,其值为一个初始化为全高清视频分辨率(1920 像素宽,1080 像素高)的<code>Resolution</code>实例。</p>
|
||
<p>然后示例中又声明了一个名为<code>cinema</code>的变量,其值为之前声明的<code>hd</code>。因为<code>Resolution</code>是一个结构体,所以<code>cinema</code>的值其实是<code>hd</code>的一个拷贝副本,而不是<code>hd</code>本身。尽管<code>hd</code>和<code>cinema</code>有着相同的宽(width)和高(height)属性,但是在后台中,它们是两个完全不同的实例。</p>
|
||
<p>下面,为了符合数码影院放映的需求(2048 像素宽,1080 像素高),<code>cinema</code>的<code>width</code>属性需要作如下修改:</p>
|
||
<pre><code>cinema.width = 2048
|
||
</code></pre><p>这里,将会显示<code>cinema</code>的<code>width</code>属性确已改为了<code>2048</code>:</p>
|
||
<pre><code>println("cinema is now \(cinema.width) pixels wide")
|
||
// 输出 "cinema is now 2048 pixels wide"
|
||
</code></pre><p>然而,初始的<code>hd</code>实例中<code>width</code>属性还是<code>1920</code>:</p>
|
||
<pre><code>println("hd is still \(hd.width ) pixels wide")
|
||
// 输出 "hd is still 1920 pixels wide"
|
||
</code></pre><p>在将<code>hd</code>赋予给<code>cinema</code>的时候,实际上是将<code>hd</code>中所储存的<code>值(values)</code>进行拷贝,然后将拷贝的数据储存到新的<code>cinema</code>实例中。结果就是两个完全独立的实例碰巧包含有相同的数值。由于两者相互独立,因此将<code>cinema</code>的<code>width</code>修改为<code>2048</code>并不会影响<code>hd</code>中的宽(width)。</p>
|
||
<p>枚举也遵循相同的行为准则:</p>
|
||
<pre><code>enum CompassPoint {
|
||
case North, South, East, West
|
||
}
|
||
var currentDirection = CompassPoint.West
|
||
let rememberedDirection = currentDirection
|
||
currentDirection = .East
|
||
if rememberDirection == .West {
|
||
println("The remembered direction is still .West")
|
||
}
|
||
// 输出 "The remembered direction is still .West"
|
||
</code></pre><p>上例中<code>rememberedDirection</code>被赋予了<code>currentDirection</code>的值(value),实际上它被赋予的是值(value)的一个拷贝。赋值过程结束后再修改<code>currentDirection</code>的值并不影响<code>rememberedDirection</code>所储存的原始值(value)的拷贝。</p>
|
||
<p><a name="classes_are_reference_types"></a></p>
|
||
<h2 id="-">类是引用类型</h2>
|
||
<p>与值类型不同,引用类型在被赋予到一个变量,常量或者被传递到一个函数时,操作的并不是其拷贝。因此,引用的是已存在的实例本身而不是其拷贝。</p>
|
||
<p>请看下面这个示例,其使用了之前定义的<code>VideoMode</code>类:</p>
|
||
<pre><code>let tenEighty = VideoMode()
|
||
tenEighty.resolution = hd
|
||
tenEighty.interlaced = true
|
||
tenEighty.name = "1080i"
|
||
tenEighty.frameRate = 25.0
|
||
</code></pre><p>以上示例中,声明了一个名为<code>tenEighty</code>的常量,其引用了一个<code>VideoMode</code>类的新实例。在之前的示例中,这个视频模式(video mode)被赋予了HD分辨率(1920*1080)的一个拷贝(<code>hd</code>)。同时设置为交错(interlaced),命名为<code>“1080i”</code>。最后,其帧率是<code>25.0</code>帧每秒。</p>
|
||
<p>然后,<code>tenEighty</code> 被赋予名为<code>alsoTenEighty</code>的新常量,同时对<code>alsoTenEighty</code>的帧率进行修改:</p>
|
||
<pre><code>let alsoTenEighty = tenEighty
|
||
alsoTenEighty.frameRate = 30.0
|
||
</code></pre><p>因为类是引用类型,所以<code>tenEight</code>和<code>alsoTenEight</code>实际上引用的是相同的<code>VideoMode</code>实例。换句话说,它们只是同一个实例的两种叫法。</p>
|
||
<p>下面,通过查看<code>tenEighty</code>的<code>frameRate</code>属性,我们会发现它正确的显示了基本<code>VideoMode</code>实例的新帧率,其值为<code>30.0</code>:</p>
|
||
<pre><code>println("The frameRate property of tenEighty is now \(tenEighty.frameRate)")
|
||
// 输出 "The frameRate property of theEighty is now 30.0"
|
||
</code></pre><p>需要注意的是<code>tenEighty</code>和<code>alsoTenEighty</code>被声明为<em>常量((constants)</em>而不是变量。然而你依然可以改变<code>tenEighty.frameRate</code>和<code>alsoTenEighty.frameRate</code>,因为这两个常量本身不会改变。它们并不<code>储存</code>这个<code>VideoMode</code>实例,在后台仅仅是对<code>VideoMode</code>实例的引用。所以,改变的是被引用的基础<code>VideoMode</code>的<code>frameRate</code>参数,而不改变常量的值。</p>
|
||
<h3 id="-">恒等运算符</h3>
|
||
<p>因为类是引用类型,有可能有多个常量和变量在后台同时引用某一个类实例。(对于结构体和枚举来说,这并不成立。因为它们作值类型,在被赋予到常量,变量或者传递到函数时,总是会被拷贝。)</p>
|
||
<p>如果能够判定两个常量或者变量是否引用同一个类实例将会很有帮助。为了达到这个目的,Swift 内建了两个恒等运算符:</p>
|
||
<ul>
|
||
<li>等价于 ( === )</li>
|
||
<li>不等价于 ( !== )</li>
|
||
</ul>
|
||
<p>以下是运用这两个运算符检测两个常量或者变量是否引用同一个实例:</p>
|
||
<pre><code>if tenEighty === alsoTenTighty {
|
||
println("tenTighty and alsoTenEighty refer to the same Resolution instance.")
|
||
}
|
||
//输出 "tenEighty and alsoTenEighty refer to the same Resolution instance."
|
||
</code></pre><p>请注意“等价于”(用三个等号表示,===) 与“等于”(用两个等号表示,==)的不同:</p>
|
||
<ul>
|
||
<li>“等价于”表示两个类类型(class type)的常量或者变量引用同一个类实例。</li>
|
||
<li>“等于”表示两个实例的值“相等”或“相同”,判定时要遵照类设计者定义定义的评判标准,因此相比于“相等”,这是一种更加合适的叫法。</li>
|
||
</ul>
|
||
<p>当你在定义你的自定义类和结构体的时候,你有义务来决定判定两个实例“相等”的标准。在章节<a href="23_Advanced_Operators.html#operator_functions">运算符函数(Operator Functions)</a>中将会详细介绍实现自定义“等于”和“不等于”运算符的流程。</p>
|
||
<h3 id="-">指针</h3>
|
||
<p>如果你有 C,C++ 或者 Objective-C 语言的经验,那么你也许会知道这些语言使用指针来引用内存中的地址。一个 Swift 常量或者变量引用一个引用类型的实例与 C 语言中的指针类似,不同的是并不直接指向内存中的某个地址,而且也不要求你使用星号(*)来表明你在创建一个引用。Swift 中这些引用与其它的常量或变量的定义方式相同。</p>
|
||
<p><a name="choosing_between_classes_and_structures"></a></p>
|
||
<h2 id="-">类和结构体的选择</h2>
|
||
<p>在你的代码中,你可以使用类和结构体来定义你的自定义数据类型。</p>
|
||
<p>然而,结构体实例总是通过值传递,类实例总是通过引用传递。这意味两者适用不同的任务。当你的在考虑一个工程项目的数据构造和功能的时候,你需要决定每个数据构造是定义成类还是结构体。</p>
|
||
<p>按照通用的准则,当符合一条或多条以下条件时,请考虑构建结构体:</p>
|
||
<ul>
|
||
<li>结构体的主要目的是用来封装少量相关简单数据值。</li>
|
||
<li>有理由预计一个结构体实例在赋值或传递时,封装的数据将会被拷贝而不是被引用。</li>
|
||
<li>任何在结构体中储存的值类型属性,也将会被拷贝,而不是被引用。</li>
|
||
<li>结构体不需要去继承另一个已存在类型的属性或者行为。</li>
|
||
</ul>
|
||
<p>合适的结构体候选者包括:</p>
|
||
<ul>
|
||
<li>几何形状的大小,封装一个<code>width</code>属性和<code>height</code>属性,两者均为<code>Double</code>类型。</li>
|
||
<li>一定范围内的路径,封装一个<code>start</code>属性和<code>length</code>属性,两者均为<code>Int</code>类型。</li>
|
||
<li>三维坐标系内一点,封装<code>x</code>,<code>y</code>和<code>z</code>属性,三者均为<code>Double</code>类型。</li>
|
||
</ul>
|
||
<p>在所有其它案例中,定义一个类,生成一个它的实例,并通过引用来管理和传递。实际中,这意味着绝大部分的自定义数据构造都应该是类,而非结构体。</p>
|
||
<p><a name="assignment_and_copy_behavior_for_collection_types"></a></p>
|
||
<h2 id="-collection-">集合(Collection)类型的赋值和拷贝行为</h2>
|
||
<p>Swift 中<code>数组(Array)</code>和<code>字典(Dictionary)</code>类型均以结构体的形式实现。然而当数组被赋予一个常量或变量,或被传递给一个函数或方法时,其拷贝行为与字典和其它结构体有些许不同。</p>
|
||
<p>以下对<code>数组</code>和<code>结构体</code>的行为描述与对<code>NSArray</code>和<code>NSDictionary</code>的行为描述在本质上不同,后者是以类的形式实现,前者是以结构体的形式实现。<code>NSArray</code>和<code>NSDictionary</code>实例总是以对已有实例引用,而不是拷贝的方式被赋值和传递。</p>
|
||
<blockquote>
|
||
<p>注意:</p>
|
||
<p>以下是对于数组,字典,字符串和其它值的<code>拷贝</code>的描述。
|
||
在你的代码中,拷贝好像是确实是在有拷贝行为的地方产生过。然而,在 Swift 的后台中,只有确有必要,<code>实际(actual)</code>拷贝才会被执行。Swift 管理所有的值拷贝以确保性能最优化的性能,所以你也没有必要去避免赋值以保证最优性能。(实际赋值由系统管理优化)</p>
|
||
</blockquote>
|
||
<h3 id="-">字典类型的赋值和拷贝行为</h3>
|
||
<p>无论何时将一个<code>字典</code>实例赋给一个常量或变量,或者传递给一个函数或方法,这个字典会即会在赋值或调用发生时被拷贝。在章节<a href="#structures_and_enumerations_are_value_types">结构体和枚举是值类型</a>中将会对此过程进行详细介绍。</p>
|
||
<p>如果<code>字典</code>实例中所储存的键(keys)和/或值(values)是值类型(结构体或枚举),当赋值或调用发生时,它们都会被拷贝。相反,如果键(keys)和/或值(values)是引用类型,被拷贝的将会是引用,而不是被它们引用的类实例或函数。<code>字典</code>的键和值的拷贝行为与结构体所储存的属性的拷贝行为相同。</p>
|
||
<p>下面的示例定义了一个名为<code>ages</code>的字典,其中储存了四个人的名字和年龄。<code>ages</code>字典被赋予了一个名为<code>copiedAges</code>的新变量,同时<code>ages</code>在赋值的过程中被拷贝。赋值结束后,<code>ages</code>和<code>copiedAges</code>成为两个相互独立的字典。</p>
|
||
<pre><code>var ages = ["Peter": 23, "Wei": 35, "Anish": 65, "Katya": 19]
|
||
var copiedAges = ages
|
||
</code></pre><p>这个字典的键(keys)是<code>字符串(String)</code>类型,值(values)是<code>整(Int)</code>类型。这两种类型在Swift 中都是值类型(value types),所以当字典被拷贝时,两者都会被拷贝。</p>
|
||
<p>我们可以通过改变一个字典中的年龄值(age value),检查另一个字典中所对应的值,来证明<code>ages</code>字典确实是被拷贝了。如果在<code>copiedAges</code>字典中将<code>Peter</code>的值设为<code>24</code>,那么<code>ages</code>字典仍然会返回修改前的值<code>23</code>:</p>
|
||
<pre><code>copiedAges["Peter"] = 24
|
||
println(ages["Peter"])
|
||
// 输出 "23"
|
||
</code></pre><h3 id="-">数组的赋值和拷贝行为</h3>
|
||
<p>在Swift 中,<code>数组(Arrays)</code>类型的赋值和拷贝行为要比<code>字典(Dictionary)</code>类型的复杂的多。当操作数组内容时,<code>数组(Array)</code>能提供接近C语言的的性能,并且拷贝行为只有在必要时才会发生。</p>
|
||
<p>如果你将一个<code>数组(Array)</code>实例赋给一个变量或常量,或者将其作为参数传递给函数或方法调用,在事件发生时数组的内容<code>不</code>会被拷贝。相反,数组公用相同的元素序列。当你在一个数组内修改某一元素,修改结果也会在另一数组显示。</p>
|
||
<p>对数组来说,拷贝行为仅仅当操作有可能修改数组<code>长度</code>时才会发生。这种行为包括了附加(appending),插入(inserting),删除(removing)或者使用范围下标(ranged subscript)去替换这一范围内的元素。只有当数组拷贝确要发生时,数组内容的行为规则与字典中键值的相同,参见章节[集合(collection)类型的赋值与复制行为](#assignment_and_copy_behavior_for_collection_types。</p>
|
||
<p>下面的示例将一个<code>整数(Int)</code>数组赋给了一个名为<code>a</code>的变量,继而又被赋给了变量<code>b</code>和<code>c</code>:</p>
|
||
<pre><code>var a = [1, 2, 3]
|
||
var b = a
|
||
var c = a
|
||
</code></pre><p>我们可以在<code>a</code>,<code>b</code>,<code>c</code>上使用下标语法以得到数组的第一个元素:</p>
|
||
<pre><code>println(a[0])
|
||
// 1
|
||
println(b[0])
|
||
// 1
|
||
println(c[0])
|
||
// 1
|
||
</code></pre><p>如果通过下标语法修改数组中某一元素的值,那么<code>a</code>,<code>b</code>,<code>c</code>中的相应值都会发生改变。请注意当你用下标语法修改某一值时,并没有拷贝行为伴随发生,因为下表语法修改值时没有改变数组长度的可能:</p>
|
||
<pre><code>a[0] = 42
|
||
println(a[0])
|
||
// 42
|
||
println(b[0])
|
||
// 42
|
||
println(c[0])
|
||
// 42
|
||
</code></pre><p>然而,当你给<code>a</code>附加新元素时,数组的长度<code>会</code>改变。
|
||
当附加元素这一事件发生时,Swift 会创建这个数组的一个拷贝。从此以后,<code>a</code>将会是原数组的一个独立拷贝。</p>
|
||
<p>拷贝发生后,如果再修改<code>a</code>中元素值的话,<code>a</code>将会返回与<code>b</code>,<code>c</code>不同的结果,因为后两者引用的是原来的数组:</p>
|
||
<pre><code>a.append(4)
|
||
a[0] = 777
|
||
println(a[0])
|
||
// 777
|
||
println(b[0])
|
||
// 42
|
||
println(c[0])
|
||
// 42
|
||
</code></pre><h3 id="-">确保数组的唯一性</h3>
|
||
<p>在操作一个数组,或将其传递给函数以及方法调用之前是很有必要先确定这个数组是有一个唯一拷贝的。通过在数组变量上调用<code>unshare</code>方法来确定数组引用的唯一性。(当数组赋给常量时,不能调用<code>unshare</code>方法)</p>
|
||
<p>如果一个数组被多个变量引用,在其中的一个变量上调用<code>unshare</code>方法,则会拷贝此数组,此时这个变量将会有属于它自己的独立数组拷贝。当数组仅被一个变量引用时,则不会有拷贝发生。</p>
|
||
<p>在上一个示例的最后,<code>b</code>和<code>c</code>都引用了同一个数组。此时在<code>b</code>上调用<code>unshare</code>方法则会将<code>b</code>变成一个唯一个拷贝:</p>
|
||
<pre><code>b.unshare()
|
||
</code></pre><p>在<code>unshare</code>方法调用后再修改<code>b</code>中第一个元素的值,这三个数组(<code>a</code>,<code>b</code>,<code>c</code>)会返回不同的三个值:</p>
|
||
<pre><code>b[0] = -105
|
||
println(a[0])
|
||
// 77
|
||
println(b[0])
|
||
// -105
|
||
println(c[0])
|
||
// 42
|
||
</code></pre><h3 id="-">判定两个数组是否共用相同元素</h3>
|
||
<p>我们通过使用恒等运算符(identity operators)( === 和 !==)来判定两个数组或子数组共用相同的储存空间或元素。</p>
|
||
<p>下面这个示例使用了“等同(identical to)” 运算符(===) 来判定<code>b</code>和<code>c</code>是否共用相同的数组元素:</p>
|
||
<pre><code>if b === c {
|
||
println("b and c still share the same array elements.")
|
||
} else {
|
||
println("b and c now refer to two independent sets of array elements.")
|
||
}
|
||
|
||
// 输出 "b and c now refer totwo independent sets of array elements."
|
||
</code></pre><p>此外,我们还可以使用恒等运算符来判定两个子数组是否共用相同的元素。下面这个示例中,比较了<code>b</code>的两个相等的子数组,并且确定了这两个子数组都引用相同的元素:</p>
|
||
<pre><code>if b[0...1] === b[0...1] {
|
||
println("These two subarrays share the same elements.")
|
||
} else {
|
||
println("These two subarrays do not share the same elements.")
|
||
}
|
||
// 输出 "These two subarrays share the same elements."
|
||
</code></pre><h3 id="-">强制复制数组</h3>
|
||
<p>我们通过调用数组的<code>copy</code>方法进行强制显性复制。这个方法对数组进行了浅拷贝(shallow copy),并且返回一个包含此拷贝的新数组。</p>
|
||
<p>下面这个示例中定义了一个<code>names</code>数组,其包含了七个人名。还定义了一个<code>copiedNames</code>变量,用以储存在<code>names</code>上调用<code>copy</code>方法所返回的结果:</p>
|
||
<pre><code>var names = ["Mohsen", "Hilary", "Justyn", "Amy", "Rich", "Graham", "Vic"]
|
||
var copiedNames = names.copy()
|
||
</code></pre><p>我们可以通过修改一个数组中某元素,并且检查另一个数组中对应元素的方法来判定<code>names</code>数组确已被复制。如果你将<code>copiedNames</code>中第一个元素从"<code>Mohsen</code>"修改为"<code>Mo</code>",则<code>names</code>数组返回的仍是拷贝发生前的"<code>Mohsen</code>":</p>
|
||
<pre><code>copiedName[0] = "Mo"
|
||
println(name[0])
|
||
// 输出 "Mohsen"
|
||
</code></pre><blockquote>
|
||
<p>注意:</p>
|
||
<p>如果你仅需要确保你对数组的引用是唯一引用,请调用<code>unshare</code>方法,而不是<code>copy</code>方法。<code>unshare</code>方法仅会在确有必要时才会创建数组拷贝。<code>copy</code>方法会在任何时候都创建一个新的拷贝,即使引用已经是唯一引用。</p>
|
||
</blockquote>
|
||
|
||
|
||
</section>
|
||
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
|
||
<a href="../chapter2/08_Enumerations.html" class="navigation navigation-prev " aria-label="Previous page: 枚举"><i class="fa fa-angle-left"></i></a>
|
||
|
||
|
||
<a href="../chapter2/10_Properties.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>
|