本人买了一台mac book pro专门学习Swift,以后会写一些学习笔记发上来。学习资料主要苹果官方发布的《The Swift Programming Language》,通过iBook可以找到。
今天学习了如何开始写第一个Swif程序。
首先下载安装Xcode 6,我目前用的是Xocde 6 beta 6。
打开Xcode,File -> New -> Playground创建一个playground,如下图:
图1:输入文件名
图2:选择保存路径
保存好后出现如下窗口:
上图中,左边是代码编辑区域,右边是实时显示代码效果的区域。在左边每敲一行代码,右边都会实时显示出来,不用编译也不用运行。真是很强大。这时就可以开始Swift之旅了。
这里对Playground进行下介绍,Playground是用于快速开发和调试的,开发好后要把这些代码要移到项目中。当然也可以用来学习Swift。下面这段是苹果网站对Playground的介绍。
Playgrounds enable the interactive experience of a script language, even though Swift compiles to highly-optimized native code. Type a line of code and the result appears immediately. If your code runs through a loop, add that line of code to the timeline assistant editor to watch its progression. Display variables in a graph, inspect each step when drawing a view, or watch an animated SpriteKit scene. When you’ve perfected your code in the playground, simply move it into your project. Playgrounds documentation includes tutorials that you can open in a playground, providing an interactive worksheet for experimentation.