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

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

2015-12-04 15:27 519 查看
Swift Playgrounds Swift操练场地

In this year’s Worldwide Developer Conference, Apple surprised all iOS developers by launching a new programming language called Swift. Swift is advertised as a “fast, modern, safe, interactive” programming language. The language is easier to learn and comes with features to make programming more productive.

Prior to the announcement of Swift, iOS apps were primarily written in Objective-C. The language has been around for more than 20 years and was chosen by Apple as the primary programming language for Mac and iOS development. I’ve talked to many aspiring iOS developers. A majority of them said Objective-C was hard to learn and its syntax looked weird. Simply put, the code scares some beginners off from learning iOS programming.

在今年的全球开发者大会上,苹果推出一种新的编程语言swift,这着实惊讶了所有的iOS开发者,swift被苹果形容为:快速,现代,安全,互动的开发语言。这门语言更容易学习并且有使程序开发更加高效的特性。

在swift发布之前,iOS的应用程序都是使用objective-c来编写的,这门语言已经有20多岁了,他同样被苹果选为为mac和iOS程序开发的主要语言。我与很多有抱负的iOS开发中聊天,大多数的人都会告诉我Objective-C是一门很难学习的语言,他的语法看起来很怪。

简而言之,从iOS开始编程之路会让人感觉到恐慌。

The release of Swift programming language is probably Apple’s answer to some of these comments. The syntax is much cleaner and easier to read. I have been programming in Swift since its beta release. I can say you're almost guaranteed to be more productive using Swift. It definitely speeds up the development process. Once you get used to Swift programming, it would be hard for you to switch back to Objective-C.

It seems to me that Swift will lure more web developers to build apps. If you’re a web developer with some programming experience on any scripting languages, you can leverage your existing expertise to gain knowledge on developing on iOS. It would be fairly easy for you to pick up Swift. That said, even if you’re a total beginner with no prior programming experience, you’ll also find the language friendlier and feel more comfortable to develop apps in Swift.

swift开发语言的发布可能是苹果公司对于这些评论的回答。她的语法更干净更容易读.我从swift的测试版本就开始使用它编写程序,我几乎可以保证当你使用swift你会感觉她更高效,她加快了你的开发进程。当你开始使用了swift编写程序,你将很难再去选择使用OC。

swift看起来会诱使更多从事web开发的程序员开始创建app。如果你就是一个用其他脚本语言并且有一些开发经验的web开发者,那么你在做iOS的开发时候就可以利用起来你现有的一些技术(经验),这对你来说得到swift技术将是非常简单的。也就是说,即使你是一个没有任何开发经验的完全的新人,你将同样可以找到码友,并且用swift开发app会让你感觉非常舒服。

To get a taste of Swift programming language, let’s take a look at the following code snippets. 为了感受一下swift开发语言,让我们通过下面一小段代码来瞧瞧到底如何。

Objective-C



Swift



The first block of code is written in Objective-C, while the second one is written in Swift. Which language do you prefer? I guess you would prefer to programming in Swift, especially if you’re frustrated with the Objective-C syntax.

第一段代码块用OC些的,而第二个代码块是用swift写的。你更喜欢哪个语言?我猜你肯定会更喜欢swift开发语言吧,特别是如果你曾经被OC的语法虐过。

Constants and variables are two basic elements in programming. In the Objective-C world, it’s your responsibility to specify the type information when declaring a variable or a constant, be it an integer or a string. For Swift, it introduces a new feature called Type Inference. You no longer need to annotate variables/constants with type information. All you need to do is to use let to declare a constant and var to declare a variable. Swift is intelligent enough to deduce the type by examining the values you provide.

常亮和变量是开发语言的两个基本元素。在OC的世界里,当你要申明一个变量或者常量的时候,你必须指定类型信息,比如Int或者string类型。但是在swift中,出现了一种新的技术叫做类型推断,你再也不用注明是类型信息。你所需要做的就是用let去申明一个常量,用var去申明一个变量。swift可以通过你提供的数值只能的推断出类型信息。

Another difference that you may notice is the omission of the semi-colon. In Objective-C, you have to end each statement with a semicolon. If you forget to do so, you will end up with an error. I know many Objective-C beginners have experienced this error before. Swift should make your developer’s life easier.

另外一个不同点可能你已经注意到了,他就是分号,在OC中,你必须在每一个语句后面加上分号。如果你忘记做了,那么你将得到一个错误。我知道在之前有很多OC初学者有这样的错误经验。swift将会使你的开发之路更简单。

Swift has added many powerful features to streamline your coding. As you can see from the above example, string manipulation is much simpler. In Objective-C, you have to choose between NSString and NSMutableString classes to indicate whether the string can be modified. You do not need to make such choice in Swift. Whenever you assign a string as variable (i.e. var), the string can be modified in your code. Concatenating strings is super easy. Just use the + operator to combine two strings. Further, Swift allows you to compare strings directly using the == operator.

swift新增了很多强大的特性来提高你的编码。从上面的例子你就可以看出,字符串的处理是变得更简单的。在OC中,你在对字符串进行处理的时候首先要考虑选择NSString还是NSMutableString来申明,但是在swift中你就不必有此烦恼。只需要用variable(简写var)就可以申请一个字符串,而且这个字符串在代码中是可以随意修改的。链接字符串是超级简单的。仅仅需要用一个加号就可以连接两者。更妙的是,swift甚至允许你直接使用==来把两个字符串进行对比。

There is no better way to explore coding than actually writing code. Xcode 6 introduces a new feature called Playgrounds. It’s an interactive development environment for developers to experiment Swift programming and allows you to see the result of your code in real-time. Assuming you’ve installed Xcode 6 (or up), launch the application (by clicking the Xcode icon in Launchpad). You’ll see a startup dialog.

没有一种方法比直接上手写代码更能探索代码世界的奥妙。Xcode6引入了一个新的特性叫做练习场。他是一个对于开发者来说测试swift程序最好的可交互的开发环境并且是你在写下代码之后立马可以看到结果的方式 。假设你已经安装好了Xcode6或以上版本,打开Xcode编程工具(通过在Launchpad里点击Xcode图标),你将可以看到一个对话框。(书中是Xcode6,我读的时候已经到7.1.1了,我直接截图自己的,不用书中的拉)



Playground is a special type of Xcode file. You can simply click “Get started with a playground” to get started. You’ll then be prompted to fill in a project name and select a platform.

训练场在xcode文件中是一种特别的类型,你可以简单的点击“Get started with a playgound”去打开它。你将马上被提示输入项目名称和选择一个平台。

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