您的位置:首页 > 移动开发 > Swift

The beginning iOS8 Programming with Swift 中文翻译 - 5

2015-12-04 16:52 561 查看


Use the default name or your own name but remember to select iOS as the platform. 使用默认的名字或者你自己的名字,但是切记要选择iOS平台

Once you confirm to save the file, Xcode opens the Playground interface. Your screen should like this: 当你确定保存文件,Xcode打开了练习场页面,你的屏幕可能会像这样:



On the left pane of the screen, it is the editor area where you type your code. As you write your code, Playground immediately interprets your code and displays the result on the right pane. By default, Playground includes two lines of code. As you can see, the result of the “str” declaration appears immediately on the right pane.

屏幕的左面板上是你写代码的编辑区域。当你写下你的代码之后,练习场会立刻解释并编译执行你的代码,随后现在结果在右面板上。默认情况下,练习场会有两行代码,就像你看到的,字符串的结果立刻出现在了右边的面板上。

We’ll write some code in Playground together. Remember the purpose of this exercise is to let you experience Swift programming and get a better idea of Xcode 6. I’ll try to explain part of the code as we move along. However, even if you do not understand any line of the code, that is completely fine especially you’re a new comer. I’m quite sure you’ll be confused by some of the terms like class, method. Forget about their meanings, just relax and play around with Xcode. We’ll go over them again in later chapters.

我们将一起在练习场写一些代码。切记这些练习的目的是让你获得swift的开发经验并且更好地了解Xcode6.我将跟着我们的代码的顺序进行解释。无论如何,即使你不太明白一些行的代码也没有关系特别是你是一个刚开始学习的人。我几乎可以确定你肯定会对像类,方法之间的关系感到迷惑,不要去想太多他们的意义,你只需要放轻松并且与Xcode愉快的玩耍,我们讲在后面的章节再次讲解他们。

Cool! Let’s get started. 酷!让我们开始吧。

First, key in a couple lines of code. Here we declare two more variables: 首先,先写几行代码吧!这里我们申明两个变量:

var message1 = "Hello swift! How can I get started?"

var message2 = "The best way of get started is to stop talking and code."

As soon as you insert the above lines of code, you will see the result on the right pane. 当你一再代码中插入以上的代码,就立刻可以在右边框里看到结果。



Let’s continue to add the following line of code: 让我们接着添加一些代码:

message1.uppercaseString

Xcode’s editor comes with an auto-complete feature. Auto-complete is a very handy feature to speed up your coding. Once you type “mess”, you’ll see an auto-complete window showing some suggestions based on what you have keyed in. All you need to do is to select “message1” and hit enter.

Xcode编译器有自动代码补全特性,自动补全对快速开发是一个非常便利的特性。当你写下“mess”,你将看到自动补全的窗口向你展示一些基于你之前写过的代码的建议。你所需要的做的只是选择“message1”然后敲一下Enter键。



Swift employs the dot syntax for calling methods and accessing the properties of a variable. As you type the dot after message1, the auto-complete window pops out again. It suggests a list of methods and properties belonged to the variable. You can continue to type “uppercaseString” or select it from the auto-complete window.

swift使用点语法来调用方法或者访问成员变量的属性。当你在message1的后面写下.的时候,自动补全界面会再次跳出,他会给出一系列的方法建议和变量的属性名,你可以直接从自动补全窗口选择你想要的。


    

Once you complete your typing, you would see the output immediately. When we use the uppercaseString property, the content of message1 is converted to upper case automatically.

当你完成了代码之后,你将立马看到输出。当我们使用了uppercaseString方法之后,message1的文本立刻变成全部大写了。

未完待续!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: