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

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

2015-12-04 19:13 507 查看
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. 这个页面去下载playground文件来作为参考。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: