Files
the-swift-programming-langu…/chapter2/10_Properties.html
2014-06-15 08:30:20 +08:00

989 lines
48 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

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

<!DOCTYPE HTML>
<html lang="en-US" manifest="../manifest.appcache">
<head prefix="og: http://ogp.me/ns# book: http://ogp.me/ns/book#">
<meta charset="UTF-8">
<title>《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 兴趣交流群307017261">
<meta name="keywords" content="gitbook,github" >
<meta name="generator" content="www.gitbook.io">
<link rel="next" href="../chapter2/11_Methods.html" />
<link rel="prev" href="../chapter2/09_Classes_and_Structures.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.10" data-basepath=".." data-revision="1402792177330">
<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: 15.789473684210526%;min-width: 13.157894736842104%;"></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 " data-progress="2.11" style="left: 18.42105263157895%;"></a>
<a href="../chapter2/12_Subscripts.html" title="下标脚本" class="chapter " data-progress="2.12" style="left: 21.05263157894737%;"></a>
<a href="../chapter2/13_Inheritance.html" title="继承" class="chapter " data-progress="2.13" style="left: 23.68421052631579%;"></a>
<a href="../chapter2/14_Initialization.html" title="构造过程" class="chapter " data-progress="2.14" style="left: 26.31578947368421%;"></a>
<a href="../chapter2/15_Deinitialization.html" title="析构过程" class="chapter " data-progress="2.15" style="left: 28.94736842105263%;"></a>
<a href="../chapter2/16_Automatic_Reference_Counting.html" title="自动引用计数" class="chapter " data-progress="2.16" style="left: 31.57894736842105%;"></a>
<a href="../chapter2/17_Optional_Chaining.html" title="可选链" class="chapter " data-progress="2.17" style="left: 34.21052631578947%;"></a>
<a href="../chapter2/18_Type_Casting.html" title="类型检查" class="chapter " data-progress="2.18" style="left: 36.8421052631579%;"></a>
<a href="../chapter2/19_Nested_Types.html" title="类型嵌套" class="chapter " data-progress="2.19" style="left: 39.473684210526315%;"></a>
<a href="../chapter2/02_Basic_Operators.html" title="基本运算符" class="chapter " data-progress="2.2" style="left: 42.10526315789474%;"></a>
<a href="../chapter2/20_Extensions.html" title="扩展" class="chapter " data-progress="2.20" style="left: 44.73684210526316%;"></a>
<a href="../chapter2/21_Protocols.html" title="协议" class="chapter " data-progress="2.21" style="left: 47.36842105263158%;"></a>
<a href="../chapter2/22_Generics.html" title="泛型" class="chapter " data-progress="2.22" style="left: 50%;"></a>
<a href="../chapter2/23_Advanced_Operators.html" title="高级操作符" class="chapter " data-progress="2.23" style="left: 52.63157894736842%;"></a>
<a href="../chapter2/03_Strings_and_Characters.html" title="字符串和字符" class="chapter " data-progress="2.3" style="left: 55.26315789473684%;"></a>
<a href="../chapter2/04_Collection_Types.html" title="集合类型" class="chapter " data-progress="2.4" style="left: 57.89473684210526%;"></a>
<a href="../chapter2/05_Control_Flow.html" title="控制流" class="chapter " data-progress="2.5" style="left: 60.526315789473685%;"></a>
<a href="../chapter2/06_Functions.html" title="函数" class="chapter " data-progress="2.6" style="left: 63.1578947368421%;"></a>
<a href="../chapter2/07_Closures.html" title="闭包" class="chapter " data-progress="2.7" style="left: 65.78947368421052%;"></a>
<a href="../chapter2/08_Enumerations.html" title="枚举" class="chapter " data-progress="2.8" style="left: 68.42105263157895%;"></a>
<a href="../chapter2/09_Classes_and_Structures.html" title="类和结构体" class="chapter " data-progress="2.9" style="left: 71.05263157894737%;"></a>
<a href="../chapter3/chapter3.html" title="语言参考" class="chapter new-chapter" data-progress="3" style="left: 73.6842105263158%;"></a>
<a href="../chapter3/01_About_the_Language_Reference.html" title="关于语言参考" class="chapter " data-progress="3.1" style="left: 76.3157894736842%;"></a>
<a href="../chapter3/09_Summary_of_the_Grammar.html" title="语法总结" class="chapter " data-progress="3.10" style="left: 78.94736842105263%;"></a>
<a href="../chapter3/02_Lexical_Structure.html" title="词法结构" class="chapter " data-progress="3.2" style="left: 81.57894736842105%;"></a>
<a href="../chapter3/03_Types.html" title="类型" class="chapter " data-progress="3.3" style="left: 84.21052631578948%;"></a>
<a href="../chapter3/04_Expressions.html" title="表达式" class="chapter " data-progress="3.4" style="left: 86.84210526315789%;"></a>
<a href="../chapter3/10_Statements.html" title="语句" class="chapter " data-progress="3.5" style="left: 89.47368421052632%;"></a>
<a href="../chapter3/05_Declarations.html" title="声明" class="chapter " data-progress="3.6" style="left: 92.10526315789474%;"></a>
<a href="../chapter3/06_Attributes.html" title="特性" class="chapter " data-progress="3.7" style="left: 94.73684210526316%;"></a>
<a href="../chapter3/07_Patterns.html" title="模式" class="chapter " data-progress="3.8" style="left: 97.36842105263158%;"></a>
<a href="../chapter3/08_Generic_Parameters_and_Arguments.html" title="泛型参数" class="chapter " data-progress="3.9" style="left: 100%;"></a>
</div>
</div>
<div class="page-inner">
<section class="normal" id="section-gitbook_606">
<blockquote>
<p>翻译shinyzhu<br>校对pp-prog </p>
</blockquote>
<h1 id="-properties-">属性 (Properties)</h1>
<hr>
<p>本页包含内容:</p>
<ul>
<li><a href="#stored_properties">存储属性Stored Properties</a></li>
<li><a href="#computed_properties">计算属性Computed Properties</a></li>
<li><a href="#property_observers">属性监视器Property Observers</a></li>
<li><a href="global_and_local_variables">全局变量和局部变量Global and Local Variables</a></li>
<li><a href="#type_properties">类型属性Type Properties</a></li>
</ul>
<p><strong>属性</strong>将值跟特定的类、结构或枚举关联。存储属性存储常量或变量作为实例的一部分,计算属性计算(而不是存储)一个值。计算属性可以用于类、结构体和枚举里,存储属性只能用于类和结构体。</p>
<p>存储属性和计算属性通常用于特定类型的实例,但是,属性也可以直接用于类型本身,这种属性称为类型属性。</p>
<p>另外,还可以定义属性监视器来监控属性值的变化,以此来触发一个自定义的操作。属性监视器可以添加到自己写的存储属性上,也可以添加到从父类继承的属性上。</p>
<p><a name="stored_properties"></a></p>
<h2 id="-">存储属性</h2>
<p>简单来说,一个存储属性就是存储在特定类或结构体的实例里的一个常量或变量,存储属性可以是<em>变量存储属性</em>(用关键字<code>var</code>定义),也可以是<em>常量存储属性</em>(用关键字<code>let</code>定义)。</p>
<p>可以在定义存储属性的时候指定默认值,请参考<a href="../chapter2/14_Initialization.html">构造过程</a>一章的<a href="../chapter2/14_Initialization.html#default_property_values">默认属性值</a>一节。也可以在构造过程中设置或修改存储属性的值,甚至修改常量存储属性的值,请参考<a href="../chapter2/14_Initialization.html">构造过程</a>一章的<a href="../chapter2/14_Initialization.html#modifying_constant_properties_during_initialization">在初始化阶段修改常量存储属性</a>一节。</p>
<p>下面的例子定义了一个名为<code>FixedLengthRange</code>的结构体,它描述了一个在创建后无法修改值域宽度的区间:</p>
<pre><code class="lang-swift">struct FixedLengthRange {
var firstValue: Int
let length: Int
}
var rangeOfThreeItems = FixedLengthRange(firstValue: 0, length: 3)
// 该区间表示整数012
rangeOfThreeItems.firstValue = 6
// 该区间现在表示整数678
</code></pre>
<p><code>FixedLengthRange</code>的实例包含一个名为<code>firstValue</code>的变量存储属性和一个名为<code>length</code>的常量存储属性。在上面的例子中,<code>length</code>在创建实例的时候被赋值,因为它是一个常量存储属性,所以之后无法修改它的值。</p>
<p><a name="stored_properties_of_constant_structure_instances"></a></p>
<h3 id="-">常量和存储属性</h3>
<p>如果创建了一个结构体的实例并赋值给一个常量,则无法修改实例的任何属性,即使定义了变量存储属性:</p>
<pre><code class="lang-swift">let rangeOfFourItems = FixedLengthRange(firstValue: 0, length: 4)
// 该区间表示整数0123
rangeOfFourItems.firstValue = 6
// 尽管 firstValue 是个变量属性,这里还是会报错
</code></pre>
<p>因为<code>rangeOfFourItems</code>声明成了常量(用<code>let</code>关键字),即使<code>firstValue</code>是一个变量属性,也无法再修改它了。</p>
<p>这种行为是由于结构体struct属于<em>值类型</em>。当值类型的实例被声明为常量的时候,它的所有属性也就成了常量。</p>
<p>属于<em>引用类型</em>的类class则不一样把一个引用类型的实例赋给一个常量后仍然可以修改实例的变量属性。</p>
<p><a name="lazy_stored_properties"></a></p>
<h3 id="-">延迟存储属性</h3>
<p>延迟存储属性是指当第一次被调用的时候才会计算其初始值的属性。在属性声明前使用<code>@lazy</code>来标示一个延迟存储属性。</p>
<blockquote>
<p>注意:<br>必须将延迟存储属性声明成变量(使用<code>var</code>关键字),因为属性的值在实例构造完成之前可能无法得到。而常量属性在构造过程完成之前必须要有初始值,因此无法声明成延迟属性。 </p>
</blockquote>
<p>延迟属性很有用,当属性的值依赖于在实例的构造过程结束前无法知道具体值的外部因素时,或者当属性的值需要复杂或大量计算时,可以只在需要的时候来计算它。</p>
<p>下面的例子使用了延迟存储属性来避免复杂类的不必要的初始化。例子中定义了<code>DataImporter</code><code>DataManager</code>两个类,下面是部分代码:</p>
<pre><code class="lang-swift">class DataImporter {
/*
DataImporter 是一个将外部文件中的数据导入的类。
这个类的初始化会消耗不少时间。
*/
var fileName = &quot;data.txt&quot;
// 这是提供数据导入功能
}
class DataManager {
@lazy var importer = DataImporter()
var data = String[]()
// 这是提供数据管理功能
}
let manager = DataManager()
manager.data += &quot;Some data&quot;
manager.data += &quot;Some more data&quot;
// DataImporter 实例的 importer 属性还没有被创建
</code></pre>
<p><code>DataManager</code>类包含一个名为<code>data</code>的存储属性,初始值是一个空的字符串(<code>String</code>)数组。虽然没有写出全部代码,<code>DataManager</code>类的目的是管理和提供对这个字符串数组的访问。</p>
<p><code>DataManager</code>的一个功能是从文件导入数据,该功能由<code>DataImporter</code>类提供,<code>DataImporter</code>需要消耗不少时间完成初始化:因为它的实例在初始化时可能要打开文件,还要读取文件内容到内存。</p>
<p><code>DataManager</code>也可能不从文件中导入数据。所以当<code>DataManager</code>的实例被创建时,没必要创建一个<code>DataImporter</code>的实例,更明智的是当用到<code>DataImporter</code>的时候才去创建它。</p>
<p>由于使用了<code>@lazy</code><code>importer</code>属性只有在第一次被访问的时候才被创建。比如访问它的属性<code>fileName</code>时:</p>
<pre><code class="lang-swift">println(manager.importer.fileName)
// DataImporter 实例的 importer 属性现在被创建了
// 输出 &quot;data.txt”
</code></pre>
<p><a name="stored_properties_and_instance_variables"></a></p>
<h3 id="-">存储属性和实例变量</h3>
<p>如果您有过 Objective-C 经验,应该知道有两种方式在类实例存储值和引用。对于属性来说,也可以使用实例变量作为属性值的后端存储。</p>
<p>Swift 编程语言中把这些理论统一用属性来实现。Swift 中的属性没有对应的实例变量,属性的后端存储也无法直接访问。这就避免了不同场景下访问方式的困扰,同时也将属性的定义简化成一个语句。
一个类型中属性的全部信息——包括命名、类型和内存管理特征——都在唯一一个地方(类型定义中)定义。</p>
<p><a name="computed_properties"></a></p>
<h2 id="-">计算属性</h2>
<p>除存储属性外,类、结构体和枚举可以定义<em>计算属性</em>,计算属性不直接存储值,而是提供一个 getter 来获取值,一个可选的 setter 来间接设置其他属性或变量的值。</p>
<pre><code class="lang-swift">struct Point {
var x = 0.0, y = 0.0
}
struct Size {
var width = 0.0, height = 0.0
}
struct Rect {
var origin = Point()
var size = Size()
var center: Point {
get {
let centerX = origin.x + (size.width / 2)
let centerY = origin.y + (size.height / 2)
return Point(x: centerX, y: centerY)
}
set(newCenter) {
origin.x = newCenter.x - (size.width / 2)
origin.y = newCenter.y - (size.height / 2)
}
}
}
var square = Rect(origin: Point(x: 0.0, y: 0.0),
size: Size(width: 10.0, height: 10.0))
let initialSquareCenter = square.center
square.center = Point(x: 15.0, y: 15.0)
println(&quot;square.origin is now at (\(square.origin.x), \(square.origin.y))&quot;)
// 输出 &quot;square.origin is now at (10.0, 10.0)”
</code></pre>
<p>这个例子定义了 3 个几何形状的结构体:</p>
<ul>
<li><code>Point</code>封装了一个<code>(x, y)</code>的坐标</li>
<li><code>Size</code>封装了一个<code>width</code><code>height</code></li>
<li><code>Rect</code>表示一个有原点和尺寸的矩形</li>
</ul>
<p><code>Rect</code>也提供了一个名为<code>center</code>的计算属性。一个矩形的中心点可以从原点和尺寸来算出,所以不需要将它以显式声明的<code>Point</code>来保存。<code>Rect</code>的计算属性<code>center</code>提供了自定义的 getter 和 setter 来获取和设置矩形的中心点,就像它有一个存储属性一样。</p>
<p>例子中接下来创建了一个名为<code>square</code><code>Rect</code>实例,初始值原点是<code>(0, 0)</code>,宽度高度都是<code>10</code>。如图所示蓝色正方形。</p>
<p><code>square</code><code>center</code>属性可以通过点运算符(<code>square.center</code>)来访问,这会调用 getter 来获取属性的值。跟直接返回已经存在的值不同getter 实际上通过计算然后返回一个新的<code>Point</code>来表示<code>square</code>的中心点。如代码所示,它正确返回了中心点<code>(5, 5)</code></p>
<p><code>center</code>属性之后被设置了一个新的值<code>(15, 15)</code>,表示向右上方移动正方形到如图所示橙色正方形的位置。设置属性<code>center</code>的值会调用 setter 来修改属性<code>origin</code><code>x</code><code>y</code>的值,从而实现移动正方形到新的位置。</p>
<p><img src="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Art/computedProperties_2x.png" alt="Computed Properties sample" width="388" height="387" /></p>
<p><a name="shorthand_setter_declaration"></a></p>
<h3 id="-setter-">便捷 setter 声明</h3>
<p>如果计算属性的 setter 没有定义表示新值的参数名,则可以使用默认名称<code>newValue</code>。下面是使用了便捷 setter 声明的<code>Rect</code>结构体代码:</p>
<pre><code class="lang-swift">struct AlternativeRect {
var origin = Point()
var size = Size()
var center: Point {
get {
let centerX = origin.x + (size.width / 2)
let centerY = origin.y + (size.height / 2)
return Point(x: centerX, y: centerY)
}
set {
origin.x = newValue.x - (size.width / 2)
origin.y = newValue.y - (size.height / 2)
}
}
}
</code></pre>
<p><a name="readonly_computed_properties"></a></p>
<h3 id="-">只读计算属性</h3>
<p>只有 getter 没有 setter 的计算属性就是<em>只读计算属性</em>。只读计算属性总是返回一个值,可以通过点运算符访问,但不能设置新的值。</p>
<p>&lt;&lt;&lt;&lt;&lt;&lt;&lt; HEAD</p>
<blockquote>
<p>注意: </p>
<h1 id="-var-let-">必须使用<code>var</code>关键字定义计算属性,包括只读计算属性,因为他们的值不是固定的。<code>let</code>关键字只用来声明常量属性,表示初始化后再也无法修改的值。 </h1>
<p>注意:</p>
<p>必须使用<code>var</code>关键字定义计算属性,包括只读计算属性,因为它们的值不是固定的。<code>let</code>关键字只用来声明常量属性,表示初始化后再也无法修改的值。</p>
<blockquote>
<blockquote>
<blockquote>
<blockquote>
<blockquote>
<blockquote>
<p>a516af6a531a104ec88da0d236ecf389a5ec72af</p>
</blockquote>
</blockquote>
</blockquote>
</blockquote>
</blockquote>
</blockquote>
</blockquote>
<p>只读计算属性的声明可以去掉<code>get</code>关键字和花括号:</p>
<pre><code class="lang-swift">struct Cuboid {
var width = 0.0, height = 0.0, depth = 0.0
var volume: Double {
return width * height * depth
}
}
let fourByFiveByTwo = Cuboid(width: 4.0, height: 5.0, depth: 2.0)
println(&quot;the volume of fourByFiveByTwo is \(fourByFiveByTwo.volume)&quot;)
// 输出 &quot;the volume of fourByFiveByTwo is 40.0&quot;
</code></pre>
<p>这个例子定义了一个名为<code>Cuboid</code>的结构体,表示三维空间的立方体,包含<code>width</code><code>height</code><code>depth</code>属性,还有一个名为<code>volume</code>的只读计算属性用来返回立方体的体积。设置<code>volume</code>的值毫无意义,因为通过<code>width</code><code>height</code><code>depth</code>就能算出<code>volume</code>。然而,<code>Cuboid</code>提供一个只读计算属性来让外部用户直接获取体积是很有用的。</p>
<p><a name="property_observers"></a></p>
<h2 id="-">属性监视器</h2>
<p><em>属性监视器</em>监控和响应属性值的变化,每次属性被设置值的时候都会调用属性监视器,甚至新的值和现在的值相同的时候也不例外。</p>
<p>可以为除了延迟存储属性之外的其他存储属性添加属性监视器,也可以通过重载属性的方式为继承的属性(包括存储属性和计算属性)添加属性监视器。属性重载请参考<a href="chapter/13_Inheritance.html">继承</a>一章的<a href="chapter/13_Inheritance.html#overriding">重载</a></p>
<blockquote>
<p>注意:<br>不需要为无法重载的计算属性添加属性监视器,因为可以通过 setter 直接监控和响应值的变化。 </p>
</blockquote>
<p>可以为属性添加如下的一个或全部监视器:</p>
<ul>
<li><code>willSet</code>在设置新的值之前调用</li>
<li><code>didSet</code>在新的值被设置之后立即调用</li>
</ul>
<p><code>willSet</code>监视器会将新的属性值作为固定参数传入,在<code>willSet</code>的实现代码中可以为这个参数指定一个名称,如果不指定则参数仍然可用,这时使用默认名称<code>newValue</code>表示。</p>
<p>类似地,<code>didSet</code>监视器会将旧的属性值作为参数传入,可以为该参数命名或者使用默认参数名<code>oldValue</code></p>
<p>&lt;&lt;&lt;&lt;&lt;&lt;&lt; HEAD</p>
<blockquote>
<p>注意: </p>
<h1 id="-willset-didset-"><code>willSet</code><code>didSet</code>监视器在属性初始化过程中不会被调用,他们只会当属性的值在初始化之外的地方被设置时被调用。 </h1>
<p>注意:</p>
<p><code>willSet</code><code>didSet</code>监视器在属性初始化过程中不会被调用,它们只会当属性的值在初始化之外的地方被设置时被调用。</p>
<blockquote>
<blockquote>
<blockquote>
<blockquote>
<blockquote>
<blockquote>
<p>a516af6a531a104ec88da0d236ecf389a5ec72af</p>
</blockquote>
</blockquote>
</blockquote>
</blockquote>
</blockquote>
</blockquote>
</blockquote>
<p>这里是一个<code>willSet</code><code>didSet</code>的实际例子,其中定义了一个名为<code>StepCounter</code>的类,用来统计当人步行时的总步数,可以跟计步器或其他日常锻炼的统计装置的输入数据配合使用。</p>
<pre><code class="lang-swift">class StepCounter {
var totalSteps: Int = 0 {
willSet(newTotalSteps) {
println(&quot;About to set totalSteps to \(newTotalSteps)&quot;)
}
didSet {
if totalSteps &gt; oldValue {
println(&quot;Added \(totalSteps - oldValue) steps&quot;)
}
}
}
}
let stepCounter = StepCounter()
stepCounter.totalSteps = 200
// About to set totalSteps to 200
// Added 200 steps
stepCounter.totalSteps = 360
// About to set totalSteps to 360
// Added 160 steps
stepCounter.totalSteps = 896
// About to set totalSteps to 896
// Added 536 steps
</code></pre>
<p><code>StepCounter</code>类定义了一个<code>Int</code>类型的属性<code>totalSteps</code>,它是一个存储属性,包含<code>willSet</code><code>didSet</code>监视器。</p>
<p><code>totalSteps</code>设置新值的时候,它的<code>willSet</code><code>didSet</code>监视器都会被调用,甚至当新的值和现在的值完全相同也会调用。</p>
<p>例子中的<code>willSet</code>监视器将表示新值的参数自定义为<code>newTotalSteps</code>,这个监视器只是简单的将新的值输出。</p>
<p><code>didSet</code>监视器在<code>totalSteps</code>的值改变后被调用,它把新的值和旧的值进行对比,如果总的步数增加了,就输出一个消息表示增加了多少步。<code>didSet</code>没有提供自定义名称,所以默认值<code>oldValue</code>表示旧值的参数名。</p>
<blockquote>
<p>注意:<br>如果在<code>didSet</code>监视器里为属性赋值,这个值会替换监视器之前设置的值。 </p>
</blockquote>
<p><a name="global_and_local_variables"></a></p>
<h2 id="-">全局变量和局部变量</h2>
<p>计算属性和属性监视器所描述的模式也可以用于<em>全局变量</em><em>局部变量</em>,全局变量是在函数、方法、闭包或任何类型之外定义的变量,局部变量是在函数、方法或闭包内部定义的变量。</p>
<p>前面章节提到的全局或局部变量都属于存储型变量,跟存储属性类似,它提供特定类型的存储空间,并允许读取和写入。</p>
<p>另外,在全局或局部范围都可以定义计算型变量和为存储型变量定义监视器,计算型变量跟计算属性一样,返回一个计算的值而不是存储值,声明格式也完全一样。</p>
<blockquote>
<p>注意:<br>全局的常量或变量都是延迟计算的,跟<a href="#lazy_stored_properties">延迟存储属性</a>相似,不同的地方在于,全局的常量或变量不需要标记<code>@lazy</code>特性。<br>局部范围的常量或变量不会延迟计算。 </p>
</blockquote>
<p><a name="type_properties"></a></p>
<h2 id="-">类型属性</h2>
<p>实例的属性属于一个特定类型实例,每次类型实例化后都拥有自己的一套属性值,实例之间的属性相互独立。</p>
<p>也可以为类型本身定义属性,不管类型有多少个实例,这些属性都只有唯一一份。这种属性就是<em>类型属性</em></p>
<p>类型属性用于定义特定类型所有实例共享的数据,比如所有实例都能用的一个常量(就像 C 语言中的静态常量),或者所有实例都能访问的一个变量(就像 C 语言中的静态变量)。</p>
<p>对于值类型指结构体和枚举可以定义存储型和计算型类型属性对于类class则只能定义计算型类型属性。</p>
<p>值类型的存储型类型属性可以是变量或常量,计算型类型属性跟实例的计算属性一样定义成变量属性。</p>
<blockquote>
<p>注意:<br>跟实例的存储属性不同,必须给存储型类型属性指定默认值,因为类型本身无法在初始化过程中使用构造器给类型属性赋值。 </p>
</blockquote>
<p><a name="type_property_syntax"></a></p>
<h3 id="-">类型属性语法</h3>
<p>在 C 或 Objective-C 中,静态常量和静态变量的定义是通过特定类型加上<code>global</code>关键字。在 Swift 编程语言中,类型属性是作为类型定义的一部分写在类型最外层的花括号内,因此它的作用范围也就在类型支持的范围内。</p>
<p>使用关键字<code>static</code>来定义值类型的类型属性,关键字<code>class</code>来为类class定义类型属性。下面的例子演示了存储型和计算型类型属性的语法</p>
<pre><code class="lang-swift">struct SomeStructure {
static var storedTypeProperty = &quot;Some value.&quot;
static var computedTypeProperty: Int {
// 这里返回一个 Int 值
}
}
enum SomeEnumeration {
static var storedTypeProperty = &quot;Some value.&quot;
static var computedTypeProperty: Int {
// 这里返回一个 Int 值
}
}
class SomeClass {
class var computedTypeProperty: Int {
// 这里返回一个 Int 值
}
}
</code></pre>
<blockquote>
<p>注意:<br>例子中的计算型类型属性是只读的,但也可以定义可读可写的计算型类型属性,跟实例计算属性的语法类似。 </p>
</blockquote>
<p><a name="querying_and_setting_type_properties"></a></p>
<h3 id="-">获取和设置类型属性的值</h3>
<p>跟实例的属性一样,类型属性的访问也是通过点运算符来进行,但是,类型属性是通过类型本身来获取和设置,而不是通过实例。比如:</p>
<pre><code class="lang-swift">println(SomeClass.computedTypeProperty)
// 输出 &quot;42&quot;
println(SomeStructure.storedTypeProperty)
// 输出 &quot;Some value.&quot;
SomeStructure.storedTypeProperty = &quot;Another value.&quot;
println(SomeStructure.storedTypeProperty)
// 输出 &quot;Another value.”
</code></pre>
<p>下面的例子定义了一个结构体,使用两个存储型类型属性来表示多个声道的声音电平值,每个声道有一个 0 到 10 之间的整数表示声音电平值。</p>
<p>后面的图表展示了如何联合使用两个声道来表示一个立体声的声音电平值。当声道的电平值是 0没有一个灯会亮当声道的电平值是 10所有灯点亮。本图中左声道的电平是 9右声道的电平是 7。</p>
<p><img src="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Art/staticPropertiesVUMeter_2x.png" alt="Static Properties VUMeter" width="243" height="357" /></p>
<p>上面所描述的声道模型使用<code>AudioChannel</code>结构体来表示:</p>
<pre><code class="lang-swift">struct AudioChannel {
static let thresholdLevel = 10
static var maxInputLevelForAllChannels = 0
var currentLevel: Int = 0 {
didSet {
if currentLevel &gt; AudioChannel.thresholdLevel {
// 将新电平值设置为阀值
currentLevel = AudioChannel.thresholdLevel
}
if currentLevel &gt; AudioChannel.maxInputLevelForAllChannels {
// 存储当前电平值作为新的最大输入电平
AudioChannel.maxInputLevelForAllChannels = currentLevel
}
}
}
}
</code></pre>
<p>结构<code>AudioChannel</code>定义了 2 个存储型类型属性来实现上述功能。第一个是<code>thresholdLevel</code>,表示声音电平的最大上限阈值,它是一个取值为 10 的常量,对所有实例都可见,如果声音电平高于 10则取最大上限值 10见后面描述</p>
<p>第二个类型属性是变量存储型属性<code>maxInputLevelForAllChannels</code>,它用来表示所有<code>AudioChannel</code>实例的电平值的最大值,初始值是 0。</p>
<p><code>AudioChannel</code>也定义了一个名为<code>currentLevel</code>的实例存储属性,表示当前声道现在的电平值,取值为 0 到 10。</p>
<p>属性<code>currentLevel</code>包含<code>didSet</code>属性监视器来检查每次新设置后的属性值,有如下两个检查:</p>
<ul>
<li>如果<code>currentLevel</code>的新值大于允许的阈值<code>thresholdLevel</code>,属性监视器将<code>currentLevel</code>的值限定为阈值<code>thresholdLevel</code></li>
<li>如果修正后的<code>currentLevel</code>值大于任何之前任意<code>AudioChannel</code>实例中的值,属性监视器将新值保存在静态属性<code>maxInputLevelForAllChannels</code>中。</li>
</ul>
<blockquote>
<p>注意:<br>在第一个检查过程中,<code>didSet</code>属性监视器将<code>currentLevel</code>设置成了不同的值,但这时不会再次调用属性监视器。 </p>
</blockquote>
<p>可以使用结构体<code>AudioChannel</code>来创建表示立体声系统的两个声道<code>leftChannel</code><code>rightChannel</code></p>
<pre><code class="lang-swift">var leftChannel = AudioChannel()
var rightChannel = AudioChannel()
</code></pre>
<p>如果将左声道的电平设置成 7类型属性<code>maxInputLevelForAllChannels</code>也会更新成 7</p>
<pre><code class="lang-swift">leftChannel.currentLevel = 7
println(leftChannel.currentLevel)
// 输出 &quot;7&quot;
println(AudioChannel.maxInputLevelForAllChannels)
// 输出 &quot;7&quot;
</code></pre>
<p>如果试图将右声道的电平设置成 11则会将右声道的<code>currentLevel</code>修正到最大值 10同时<code>maxInputLevelForAllChannels</code>的值也会更新到 10</p>
<pre><code class="lang-swift">rightChannel.currentLevel = 11
println(rightChannel.currentLevel)
// 输出 &quot;10&quot;
println(AudioChannel.maxInputLevelForAllChannels)
// 输出 &quot;10&quot;
</code></pre>
</section>
</div>
</div>
</div>
<a href="../chapter2/09_Classes_and_Structures.html" class="navigation navigation-prev " aria-label="Previous page: 类和结构体"><i class="fa fa-angle-left"></i></a>
<a href="../chapter2/11_Methods.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>