Files
the-swift-programming-langu…/chapter2/article_1.html
2014-06-04 19:39:05 +08:00

301 lines
13 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>基础部分 | Swift 编程语言</title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<meta name="robots" content="index, follow">
<meta name="author" content="">
<meta name="description" content="Swift 是苹果在 WWDC 2014 上发布的一款全新的编程语言,本书译自苹果官方的 Swift 教程《The Swift Programming Language》。">
<meta name="keywords" content="gitbook,github" >
<meta name="generator" content="www.gitbook.io">
<link rel="prev" href="../chapter2/the_basics.html" />
<meta property="og:title" content="基础部分 | Swift 编程语言">
<meta property="og:site_name" content="Swift 编程语言">
<meta property="og:type" content="book">
<meta property="og:locale" content="en_US">
<meta property="book:author" content="https://github.com/">
<meta property="book:tag" content="GitBook">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
</head>
<body>
<link rel="stylesheet" href="../gitbook/style.css">
<div class="book" data-level="2.1" data-basepath=".." data-revision="1401881908280">
<div class="book-header">
<!-- Actions Left -->
<a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>
<a href="https://github.com/null" target="_blank" class="btn pull-left home-bookmark" aria-label="GitHub home"><i class="fa fa-bookmark-o"></i></a>
<a href="#" class="btn pull-left toggle-search" aria-label="Toggle search"><i class="fa fa-search"></i></a>
<span id="font-settings-wrapper">
<a href="#" class="btn pull-left toggle-font-settings" aria-label="Toggle font settings"><i class="fa fa-font"></i>
</a>
<div class="dropdown-menu font-settings">
<div class="dropdown-caret">
<span class="caret-outer"></span>
<span class="caret-inner"></span>
</div>
<div class="btn-group btn-block">
<button id="reduce-font-size" class="btn btn-default">A</button>
<button id="enlarge-font-size" class="btn btn-default">A</button>
</div>
<ul class="list-group font-family-list">
<li class="list-group-item" data-font="0">Serif</li>
<li class="list-group-item" data-font="1">Sans</li>
</ul>
<div class="btn-group btn-group-xs btn-block color-theme-list">
<button type="button" class="btn btn-default" id="color-theme-preview-0" data-theme="0">White</button>
<button type="button" class="btn btn-default" id="color-theme-preview-1" data-theme="1">Sepia</button>
<button type="button" class="btn btn-default" id="color-theme-preview-2" data-theme="2">Night</button>
</div>
</div>
</span>
<!-- Actions Right -->
<a href="#" target="_blank" class="btn pull-right google-plus-sharing-link sharing-link" data-sharing="google-plus" aria-label="Share on Google Plus"><i class="fa fa-google-plus"></i></a>
<a href="#" target="_blank" class="btn pull-right facebook-sharing-link sharing-link" data-sharing="facebook" aria-label="Share on Facebook"><i class="fa fa-facebook"></i></a>
<a href="#" target="_blank" class="btn pull-right twitter-sharing-link sharing-link" data-sharing="twitter" aria-label="Share on Twitter"><i class="fa fa-twitter"></i></a>
<!-- Title -->
<h1>
<i class="fa fa-spinner fa-spin"></i>
<a href="../" >Swift 编程语言</a>
</h1>
</div>
<div class="book-summary">
<div class="book-search">
<input type="text" placeholder="Search" class="form-control" />
</div>
<ul class="summary">
<li data-level="0" data-path="index.html">
<a href="../"><i class="fa fa-check"></i> Introduction</a>
</li>
<li class="chapter " data-level="1" data-path="chapter1/README.html">
<a href="../chapter1/README.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/swift.html">
<a href="../chapter1/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/a_swift_tour.html">
<a href="../chapter1/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/the_basics.html">
<a href="../chapter2/the_basics.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/article_1.html">
<a href="../chapter2/article_1.html">
<i class="fa fa-check"></i> <b>2.1.</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>
</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: 100%;min-width: 80%;"></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/README.html" title="欢迎使用 Swift" class="chapter done new-chapter" data-progress="1" style="left: 20%;"></a>
<a href="../chapter1/swift.html" title="关于 Swift" class="chapter done " data-progress="1.1" style="left: 40%;"></a>
<a href="../chapter1/a_swift_tour.html" title="Swift 初见" class="chapter done " data-progress="1.2" style="left: 60%;"></a>
<a href="../chapter2/the_basics.html" title="Swift 教程" class="chapter done new-chapter" data-progress="2" style="left: 80%;"></a>
<a href="../chapter2/article_1.html" title="基础部分" class="chapter done " data-progress="2.1" style="left: 100%;"></a>
</div>
</div>
<div class="page-inner">
<section class="normal" id="section-gitbook_144">
<h1 id="-">基础部分</h1>
<p>Swift 是 iOS 和 OS X 应用开发的一门新语言。然而,如果你有 C 或者 Objective-C 开发经验的话,你会发现 Swift 的很多内容都是你熟悉的。</p>
<p>Swift 的类型是在 C 和 Objective-C 的基础上提出的,<code>Int</code>是整型;<code>Double</code><code>Float</code>是浮点型;<code>Bool</code>是布尔型;<code>String</code>是字符串。Swift 还有两个有用的集合类型,<code>Array</code><code>Dictionary</code>,详情参见<code>集合类型(待添加链接)</code></p>
<p>就像 C 语言一样Swift 使用变量来进行存储并通过变量名来关联值。在 Swift 中,值不可变的变量有着广泛的应用,它们就是常量,而且比 C 语言的常量更强大。在 Swift 中,如果你要处理的值不需要改变,那使用常量可以让你的代码更加安全并且更好地表达你的意图。</p>
<p>除了我们熟悉的类型Swift 还增加了 Objective-C 中没有的类型比如元组Tuple。元组可以让你创建或者传递一组数据比如作为函数的返回值时你可以用一个元组可以返回多个值。</p>
<p>Swift 还增加了可选Optional类型用于处理值缺失的情况。可选表示“那儿有一个值并且它等于 x ”或者“那儿没有值”。可选有点像在 Objective-C 中使用<code>nil</code>,但是它可以用在任何类型上,不仅仅是类。可选类型比 Objective-C 中的<code>nil</code>指针更加安全也更具表现力,它是 Swift 许多强大特性的重要组成部分。</p>
<p>Swift 是一个类型安全的语言可选就是一个很好的例子。Swift 可以让你清楚地知道值的类型。如果你的代码期望得到一个<code>String</code>,类型安全会阻止你不小心传入一个<code>Int</code>。你可以在开发阶段尽早发现并修正错误。</p>
<h2 id="-">常量和变量</h2>
<p>常量和变量把一个名字(比如<code>maximumNumberOfLoginAttempts</code>或者<code>welcomeMessage</code>)和一个指定类型的值(比如数字<code>10</code>或者字符串<code>Hello</code>)联系起来。常量的值一旦设定就不能改变,而变量的值可以随意更改。</p>
<h3 id="-">声明常量和变量</h3>
<p>常量和变量必须在使用前声明,用<code>let</code>来声明常量,用<code>var</code>来声明变量。下面的例子展示了如何用常量和变量来记录用户尝试登录的次数:</p>
<pre><code>let maximumNumberOfLoginAttempts = 10
var currentLoginAttempt = 0
</code></pre><p>这两行代码可以被理解为:</p>
<p>“声明一个名字是<code>maximumNumberOfLoginAttempts</code>的新常量,并给它一个值<code>10</code>。然后,声明一个名字是<code>currentLoginAttempt</code>的变量并将它的值初始化为0.”</p>
<p>在这个例子中,允许的最大尝试登录次数被声明为一个常量,因为这个值不会改变。当前尝试登录次数被声明为一个变量,因为每次尝试登录失败的时候都需要增加这个值。</p>
<p>你可以在一行中声明多个常量或者多个变量,用逗号隔开:</p>
<pre><code>var x = 0.0, y = 0.0, z = 0.0
</code></pre><blockquote>
<p>注意:如果你的代码中有不需要改变的值,最好将它声明为常量。只将需要改变的值声明为变量。</p>
</blockquote>
<h3 id="-">类型标注</h3>
<p>当你声明常量或者变量的时候可以加上类型标注,说明常量或者变量中要存储的值的类型。如果要添加类型标注,在常量或者变量名后面加上一个冒号和空格,然后加上类型名称。</p>
<p>这个例子给<code>welcomeMessage</code>变量添加了类型标注,表示这个变量可以存储<code>String</code>类型的值:</p>
<pre><code>var welcomeMessage: String
</code></pre><p>声明中的冒号代表着“是...类型”,所以这行代码可以被理解为::</p>
<p>“声明一个类型为<code>String</code>,名字为<code>welcomeMessage</code>的变量。”</p>
<p>“类型为<code>String</code>”的意思是“可以存储任意<code>String</code>类型的值。”</p>
<p><code>welcomeMessage</code>变量现在可以被设置成任意字符串:</p>
<pre><code>welcomeMessage = &quot;Hello&quot;
</code></pre><blockquote>
<p>注意一般来说你很少需要写类型标注。如果你在声明常量或者变量的时候赋了一个初始值Swift可以推断出这个常量或者变量的类型详情参见<code>类型安全和类型推断(待添加链接)</code>。在上面的例子中,没有给<code>welcomeMessage</code>赋初始值,所以添加了一个类型标注。</p>
</blockquote>
<h3 id="-">常量和变量的命名</h3>
<p>(未完)</p>
</section>
</div>
</div>
</div>
<a href="../chapter2/the_basics.html" class="navigation navigation-prev navigation-unique" aria-label="Previous page: Swift 教程"><i class="fa fa-angle-left"></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>
</body>
</html>