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

Continue to type the following line of code:   接着之前代码写下:

message2.lowercaseString + " Okay, i‘m working on it"

Swift allows you to concatenate two strings with the + operator. The line of code converts the content of message2 into lower case and then concatenated with another string. Interestingly, you can include emoji characters in your code. You may wonder how to type emoji characters in Mac OS. It’s easy. Just press control-command-spacebar and an emoji picker will be displayed.

swift允许你用+连接两个字符串。刚写的代码把message2的文本转换成小写,然后与另外的字符串连接。有趣的是,你可以连接表情符号在你的代码中。你可能想知道怎么在Mac OS中打出表情符号。其实很简单,仅仅需要按住control加command加空格键,就可以让表情符号选择器显示出来。

Let’s continue to type the following code snippet:   让我们继续编写代码 (在书中打印的方法是println(),但是后面苹果改为print()了,所以我这里按照最新的来写)

if message1 == message2{

print("Same message")

}else{

print("Not the same message")

}

Conditional logic is very common in programming. Sometimes, you want to execute certain part of code only when a certain condition is met. An if-else statement is one of the ways in Swift to control program flow. Here we compare the content of message1 and message2 variables using the == operator. If they’re equal, the program prints “Same message”. Otherwise, it prints “Not the same message”. You should see the following result on your screen.

逻辑判断语句在开发中是非常易见的。有时候你只想要在符合某些条件的情况下执行一段代码,在swift中if-else是其中一种方式来执行控制流。在这段代码中我们用==对比了message1和message2的文本。如果他们相等则打印“Same message”,如果是其他情况,则打印“Not same message”,然后你将会在你的屏幕上看到结果。

Let’s have some more fun and create a label, which is a common user interface element:  让我们创建一个Label控件并从中获取一些乐趣吧,这种控件是很常见的与用户交互的控件。

let messageLabel = UILabel(frame: CGRect(x: 0,y: 0,width: 300,height: 50))  ps:这里书中的创建方式是:UILabel(frame: CGRectMake(0, 0, 300, 50))

messageLabel.text = message1

messageLabel

Here we use a built-in UILabel class to create a label and set its size to 300x50 points. We then set its text to message1. To preview a UI element in Playground, you can click the Quick Look or Value History icon. The Quick Look feature displays the label as a pop-over. If you use the Value History feature, Playground opens a separate preview pane.

这里我们创建了一个Label控件,并且设置他的宽为300高为50,然后我们设置他显示的文字为message1的文本。如果你想在playground里预览控件,你可以点击快速查看或者数值历史图片。快速查看显示控件的方式是弹窗。如果你使用数值历史特性,那么playground将单独打开一个窗。

It’s just a plain label. Wouldn’t be great if you can change its color? Even better, you just need one line of code to customize the color. What’s more you can easily center the text and change the label to round corner. Type the following lines and you’ll see a rounded corner label with orange background.

这只是一个简单的Label,如果你可以改变他的颜色你会不会觉得很屌?更屌的是,你仅仅只需要加一行代码就可以自定义他的颜色。还有就是你还可以很简单的就改变他的文字的位置,让文字居中,让Label边角变圆。敲入下面的代码你将看到一个有着橘色背景色的圆角Label;

messageLabel.backgroundColor = UIColor.orangeColor()

messageLabel.textAlignment = NSTextAlignment.Center

messageLabel.layer.cornerRadius = 10.0

messageLabel.clipsToBounds = true

messageLabel

This is the power of iOS SDK. It comes with tons of pre-built elements and allows developers to customize them with few lines of code.

这就是iOS SDK的厉害之处。他提供了大量的预先构建的控件并且允许开发者自定义他们确只需要几行代码

Don’t get me wrong. Typically you do not need to write code to create the user interface. Xcode provides a Storyboard feature that lets you design UI using drag-and-drop. We’ll go through in the next chapter.

别误会我的意思。其实你不需要用代码来创建你的交互界面。Xcode提供了Storyboard来让你可以用拖拽的方式设计交互页面。我们将在下一章讲解。

So you’ve got a taste of Swift? What do you think? Love it? I hope you find Swift a lot easier to learn and code. Most importantly, I hope it don’t scare you away from learning app development. Next up, you’ll learn how to build your first app.

所以你现在已经体验到swift了嘛?你觉得如何?是否爱上他了?我希望你可以发现其实他是很容易学习和编写的。最重要的是,我希望你别被学习APP开发给吓到了。最后一局,你将在下一张开始学习创建你的第一个APP。

For your reference, you can download the Playground file from https://www.dropbox.com/ s/y9plgddbsjauhqq/MyPlayground.zip?dl=0.

你可以从https://www.dropbox.com/ s/y9plgddbsjauhqq/MyPlayground.zip?dl=0. 这个页面去下载playgrund文件来作为参考。

时间: 2024-07-28 14:33:48

The beginning iOS8 Programming with Swift 中文翻译 - 6的相关文章

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

Audience 读者说 This book is written for beginners without any prior programming experience and those   这本书是写给那些想要学习swift语言但之前没有过任何开发经验的初学者 who want to learn Swift programming. Whether you are a programmer who wants to learn   无论你是想学习一门新的语言的程序员或者是一个想把自己

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

周末两天没有更新,今天继续! 1  Build Your First App  创建你的第一个App “The way to get started is to quit talking and begin doing.”     开始就是放弃高谈阔论,开始脚踏实地! – Walt Disney        - 沃尔特·迪斯尼(迪士尼创始人) Hello World! Build Your First App Using Swift    你好,世界!用swift创建你的第一个App By n

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

前段时间回趟老家(奶奶过世)所以,几天没更新...接下来继续.! Familiarize Yourself with Xcode Workspace   使自己熟悉Xcode的工作空间 Before we move on to the coding part, let’s take a few minutes to have a quick look at the Xcode workspace environment. In the left pane is the project navig

《The Swift Programming Language》中文翻译及读书笔记-page29

·        第29页 数字字面上可以用下划线_,目的是增强数值的阅读性. 例如在表示1000时,一般人这样写1000,  但财务人员这样写1,000大家都知道这是1000. 故在swift语言里可以在数字间插入下划线来增强阅读性. eg: let oneMillion = 1_000_000  => 常量壹佰万 <The Swift Programming Language>中文翻译及读书笔记-page29

Functional Programming in Javascript 中文翻译 —— 目录和介绍

原著:[美] Dan Mantyla 目录 前言1 Javascript函数式编程的力量--举个例子2 函数式编程基础3 建立函数式编程环境4 在Javascript中实现函数式编程的技术5 类型理论6 高级主题以及Javascript的缺陷7 Javascript中的函数式和面型对象编程8 Javascript中的函数式和面型对象编程 关于翻译的这本书 现在市面上有两本专注于javascript函数式编程的书,一本是<Functional Javascript>(下文简称FJS), 另一本就

《Swift编程语言教程》中文翻译及读书笔记page21

<The Swift Programming Language>中文翻译及读书笔记,附件中为英文原版教程 因21页之前内容和技术关系不大,不做翻译整理,从第21页开始 第21页 1 swift作为一门语言对的新老语言的继承与扩展 本页主要内容是简要介绍了一下swift编程语言的定位.拥有自己的数据类型int.float.string等数据类型外,swift还具有array.dict等数据类型. 2 引入了tuple元组数据类型 特别说明的是在swift语言里引入了oc和c没有的tuple元组数

《Swift编程语言》中文翻译及读书笔记page21

·<The Swift Programming Language>中文翻译及读书笔记,附件中为英文原版教程 因21页之前内容和技术关系不大,不做翻译整理,从第21页开始 第21页 1 swift作为一门语言对的新老语言的继承与扩展 本页主要内容是简要介绍了一下swift编程语言的定位.拥有自己的数据类型int.float.string等数据类型外,swift还具有array.dict等数据类型. 2 引入了tuple元组数据类型 特别说明的是在swift语言里引入了oc和c没有的tuple元组

《Swift编程语言》中文翻译及读书笔记page22

·<The Swift Programming Language>中文翻译及读书笔记,附件中为英文原版教程 因21页之前内容和技术关系不大,不做翻译整理,从第21页开始 ·        第22页 本页知识点总结 1 如何在一行里定义多个变量? var开始.等号赋值.用逗号间隔 eg1: var x = 19.4, y = 18.4 z = 7.311 需要注意的是最好是同类型数据 2 如何指定数据的类型? 用冒号间隔变量名和类型名 eg2: var  x : Int = 12 eg3:var

《Swift编程语言》中文翻译及读书笔记page23

·<The Swift Programming Language>中文翻译及读书笔记,附件中为英文原版教程 因21页之前内容和技术关系不大,不做翻译整理,从第21页开始 · 第23页 1 本页主要讲述如何给常量和变量取名,即命名规则. 1)不可使数学符号.箭头 2)不可点.线等符号 3)不可以数字开始作为变量名 到底怎样才可以取一个合法的名字呢?这和其他语言一样可依据匈牙利命名法来去变量和常量名 匈牙利命名法:http://baike.baidu.com/view/419474.htm?fr=