by Colin Smith

通过科林·史密斯

Swift与Objective-C:与恐龙有关的趋势 (Swift vs. Objective-C: The trending up-and-comer vs. the dinosaur)

Swift的简短历史 (A short history of Swift)

I remember how pivotal it was when Swift was introduced at Apple’s 2014 WWDC (Worldwide Developers Conference). It was the talk of the town and all the devs I worked with couldn’t wait to try it out. The iOS community was buzzing and there was a lot of excitement around the new language.

我记得在Apple的2014年WWDC(全球开发者大会)上推出Swift时,它具有多么关键的意义。 那是小镇的话题,与我合作的所有开发人员都迫不及待地想尝试一下。 iOS社区嗡嗡作响,新语言引起了很多兴奋。

It was developed in order to carry on some concepts we saw in Objective-C such as extensible programming. But it pushed towards a different approach to coding with the protocol-oriented design and increased safety with static typing.

开发它是为了继承我们在Objective-C中看到的一些概念,例如可扩展编程。 但是,它采用了面向协议的设计,从而寻求了一种不同的编码方法,并通过静态类型提高了安全性。

It was a huge hit and saw its growth sky rocket in the years after introduction. It was the most loved programming language in 2015, the second most loved in 2016, the 11th most popular programming language in 2017, beating out Objective-C, and it also beat out Objective-C in 2018.

在推出后的数年中,这是一次巨大的成功,并见证了它的增长。 这是最喜欢 2015年的编程语言中, 第二个最喜爱的2016年,第11届最流行的 2017年的编程语言,击败了Objective-C的,也击败了 2018年的Objective-C。

Swift is also a bet by Apple on winning over novices to become iOS developers. The hope is that new developers will learn the language and use it to build iOS apps. This then increases the ecosystem of the app store. Since Swift is optimized to work with iOS apps, this ensures the apps being written are of high quality.

Swift也是苹果公司的赌注,旨在赢得新手成为iOS开发人员。 希望新的开发人员将学习该语言并将其用于构建iOS应用。 然后,这增加了应用商店的生态系统。 由于Swift经过优化可与iOS应用程序一起使用,因此可以确保所编写的应用程序具有高质量。

Swifts popularity only continues to increase, especially for smaller apps and start-ups. The gap between Swift and Objective-C will only continue to grow. The future is bright for this young language.

Swifts的受欢迎程度只会继续增加,尤其是对于较小的应用程序和初创公司。 Swift和Objective-C之间的差距只会继续扩大。 这种年轻语言的前途一片光明。

Objective-C的简短历史 (A short history of Objective-C)

Objective-C is an object-oriented programming language that is a superset of C, as the name of the language might reveal. This means that any valid C program will compile with an Objective-C compiler. It derives all its non-object oriented syntax from C and its object oriented syntax from SmallTalk. It was developed in 1984, so it has had time to mature as a language and is much more stable than Swift.

正如语言的名称所揭示的那样,Objective-C是一种面向对象的编程语言,是C的超集。 这意味着任何有效的C程序都将使用Objective-C编译器进行编译。 它从C导出所有非面向对象的语法,并从SmallTalk导出其面向对象的语法。 它开发于1984年,因此已经有一段时间成为一种语言,并且比Swift稳定得多。

Most people know Objective-C as the language that is used to develop apps for the iPhone, but the history goes much deeper than that. I’d recommend reading this article for a more in-depth look.

大多数人都知道Objective-C是用于为iPhone开发应用程序的语言,但是历史远不止于此。 我建议阅读本文以更深入地了解。

Swift的优势 (The strengths of Swift)

Swift has grown tremendously in popularity for a few key reasons. First off, there are a lot of great development tools Apple has provided to work in conjunction with Swift. One of my personal favorites is the Playground, which is only compatible with Swift. Apple introduced Playgrounds in 2016. They were introduced as a way to learn how to code, but I loved them for a different reason.

出于几个关键原因,Swift已Swift普及。 首先,苹果提供了许多很棒的开发工具来与Swift一起使用。 我个人最喜欢的游戏之一是Playground,仅与Swift兼容。 苹果于2016年推出了Playgrounds。 引入它们是一种学习编码的方法,但是出于不同的原因,我喜欢它们。

Mobile development has always had more roadblocks than web development. You need a simulator, you usually need a proprietary Integrated Development Environment (IDE), and you need to set up a whole project just to test some small prototype. In Apple’s case, you also need a developer account. The nice thing about Playgrounds is you get around some of this. You do need Xcode or the Playgrounds app, but that is all. And you can get started with coding and compiling your code right away.

移动开发始终比Web开发具有更多的障碍。 您需要一个模拟器,通常需要一个专有的集成开发环境(IDE),并且需要设置一个整个项目来测试一些小型原型。 在Apple的情况下,您还需要一个开发人员帐户。 Playgrounds的好处是您可以解决一些问题。 您确实需要Xcode或Playgrounds应用程序,仅此而已。 您可以立即开始编码和编译代码。

Yet, another huge advantage of Swift is the fact that it is open source. If you have ever wondered how a programming language worked under the hood, then you can go see for yourself! This is a great way to understand the programming language you work with daily on a deeper level.

然而,Swift的另一个巨大优势是它是开源的。 如果您曾经想知道编程语言是如何工作的,那么您可以自己看看 ! 这是更好地了解您每天使用的编程语言的好方法。

An honorable mention goes to a nice utility only available to Swift, the Swift Package Manager. The Swift Package Manager is simply a dependency manager that is integrated with the Swift build system. It isn’t a game changer by any means, since CocoaPods and Carthage were doing this job a long time ago, but it’s another solution available if needed.

值得一提的是,一个不错的实用程序仅适用于Swift( Swift软件包管理器) 。 Swift软件包管理器只是与Swift构建系统集成的依赖项管理器 。 由于CocoaPods和Carthage很久以前就从事这项工作,因此无论如何都不会改变游戏规则,但是如果需要的话,它是另一个可用的解决方案。

A lot of evidence here supports the fact that Apple is doing a lot to make Swift more desirable as the programming language of choice for iOS developers. They are creating nice utilities and auxiliaries to entice people to start using the language. This shows that Apple is pushing for Swift in full force.

这里有许多证据支持这样的事实,即苹果正在做出很多努力,使Swift更受iOS开发人员的青睐。 他们正在创建实用的工具和辅助工具,以诱使人们开始使用该语言。 这表明苹果正在全力推动Swift。

语言特征 (Language features)

Let’s get into some of the details of the language itself. Swift is safer due to its static typing and the use of optionals. In Swift, if your code requires a string, the features of Swift will guarantee that your code gets a string and not another type, such as an int. This of course depends on if you’re using the language as it is intended and not force unwrapping everything.

让我们深入探讨一下语言本身的一些细节。 Swift由于其静态类型和使用可选参数而更加安全。 在Swift中,如果您的代码需要字符串,则Swift的功能将确保您的代码获取字符串,而不是其他类型(例如int)。 当然,这取决于您是否按预期使用该语言,而不是强制展开所有内容。

Another great feature of Swift is its syntax. Especially compared to Objective-C. The best word to describe the syntax would be “succinct”. There is no need for semi-colons, calls to self or parentheses around if statements. It feels like you are skipping a lot of things that you don’t really need anyway. It can make the process of typing a lot of code “flow” better.

Swift的另一个强大功能是语法。 特别是与Objective-C相比。 描述语法的最佳词是“简洁的”。 不需要分号,对self的调用或if语句周围的括号。 感觉就像您正在跳过很多您本来不需要的东西。 它可以使键入许多代码“流”的过程更好。

Some people say this leads to development velocity improvements, but I wouldn’t exactly say that myself. The continual need to unwrap objects to comply with Swifts type-safety offsets the development gains that come with the succinctness.

有人说这会导致开发速度的提高,但是我自己并不能完全说出来。 不断需要拆开对象以符合Swifts类型安全性的需求抵消了简洁带来的开发收益。

Swift also has a lot of great control flow options with guard, if-let, advanced switch statements, repeat-while and defer. I like all the different options because it lets people control the flow of their code in a way that makes sense to them. A lot of people hate defers but love guards and vice versa. It doesn’t really matter what you like or dislike, but the options are there and you can code in the way that feels best to you.

Swift还具有很多出色的控制流选项,包括后卫,if-let,高级switch语句,while-while和defer。 我喜欢所有不同的选项,因为它使人们可以以对他们有意义的方式控制代码流。 许多人讨厌延缓延误,但是爱护卫兵,反之亦然。 您喜欢或不喜欢的对象并不重要,但是可以使用这些选项,并且可以按照最适合自己的方式进行编码。

I can’t forget all the functional programming features such as filter, map and reduce. This is great for handling collections and comes in handy quite often.

我不能忘记所有功能性的编程功能,例如过滤,映射和归约。 这非常适合处理集合,并且经常派上用场。

弱点 (The weaknesses)

Swift is a young language, and with that, comes some shifting. The migrations between versions are simply a pain. At a small company, the migration tool provided by Apple can be helpful and cover most cases. It becomes less helpful the more code you have. It’s even worse if your codebase contains both Objective-C and Swift code that interoperate.

Swift是一门年轻的语言,随之而来的是一些转变。 版本之间的迁移简直是痛苦。 在一家小公司中,Apple提供的迁移工具可能会有所帮助,并且可以解决大多数情况。 您拥有的代码越多,帮助就越小。 如果您的代码库同时包含可互操作的Objective-C和Swift代码,则情况更糟。

At my last company, the migration effort took a dedicated group a whole weekend to do. They had to do it on the weekend so that they wouldn’t run into merge conflicts from other devs pushing code. This was incredibly painful for everyone involved.

在我上一家公司,迁移工作整个周末花了一个专门小组。 他们必须在周末这样做,以免遇到其他开发人员在推动代码时的合并冲突。 这对于每个参与人员来说都非常痛苦。

A reason for these migrations is the fact that Swift isn’t ABI stable. That means newer versions of Swift cannot work with older versions of Swift. That also means that the language cannot be packaged with the OS. This is a big deal for companies with large apps that actively combat app size because Swift is being bundled with the app and increasing the size.

进行这些迁移的原因是Swift 的ABI不稳定 。 这意味着新版本的Swift无法与旧版本的Swift一起使用。 这也意味着该语言无法与OS打包在一起。 对于拥有大型应用程序且积极应对应用程序尺寸的公司而言,这是一笔大买卖,因为Swift已与应用程序捆绑在一起并增加了尺寸。

Another issue is that Swift does not play well with Xcode. Xcode feels very choppy when working with Swift and autocomplete simply doesn’t work sometimes. This is strange given how hard Apple is pushing Swift. You would think that they would want make the experience of using Swift with Xcode a delight.

另一个问题是,Swift无法与Xcode配合使用。 Xcode在使用Swift时感觉非常不稳定,有时自动完成功能根本不起作用 。 考虑到苹果在推动Swift的努力,这很奇怪。 您可能会认为,他们希望使Xcode结合使用Swift的体验。

Swift also has problems with string handling, see the code example above. It is clunky as hell. In your day to day, this isn’t too bad. Where this comes into play the most is during interviews. Unfortunately for Swift devs, interviewers love asking questions that involve string manipulation. This is compounded by the fact that the way strings are handled has changed between versions of Swift.

Swift在字符串处理方面也有问题,请参见上面的代码示例。 笨拙地狱。 在您的日常工作中,这还算不错。 发挥作用最大的是采访期间。 对于Swift开发人员而言,不幸的是,访问员喜欢询问涉及字符串操作的问题。 在不同版本的Swift之间,处理字符串的方式有所不同,这使情况更加复杂。

Objective-C的优势 (The strengths of Objective-C)

Objective-C is a highly dynamic, object oriented language. It is dynamic to the point that you can swap out method invocations at runtime using techniques like Swizzling. It is able to do these kinds of thing due to its message sending paradigm. This lets objects send messages to other objects at run time to determine the invocation of the method being called.

Objective-C是一种高度动态的,面向对象的语言。 动态的是,您可以在运行时使用Swizzling之类的方法交换方法调用。 由于其消息发送范例,它能够执行此类操作。 这样,对象可以在运行时将消息发送到其他对象,以确定调用的方法。

In practical purposes, what does this mean? Well, one big advantage is adaptability at runtime. This means accessing private APIs or doing things like mocking objects at runtime become possible. This can be especially useful when it comes to unit testing. Libraries like OCMock make this even easier and allow for very elaborate test set ups. Having good unit tests will make your app more stable and reliable.

实际上,这是什么意思? 好吧,一大优势是运行时的适应性。 这意味着可以在运行时访问私有API或执行诸如模拟对象之类的操作。 对于单元测试,这可能特别有用。 像OCMock这样的库使此操作变得更加容易,并允许进行非常复杂的测试设置。 拥有良好的单元测试将使您的应用程序更加稳定和可靠。

Speaking of stability, Objective-C has been around for a long time which makes it a very stable language. With Swift, you’ll run into bugs that are pretty surprising and would be disruptive to the stability of your app. In the example I linked above, this crash would be caused by the actual language you are using to code your app, not due to any error created by the code you wrote. This can be frustrating.

说到稳定性,Objective-C已经存在很长时间了,这使其成为非常稳定的语言。 使用Swift,您将遇到一些令人惊讶的错误,这些错误会破坏应用程序的稳定性。 在上面链接的示例中,此崩溃是由您用来编写应用程序代码的实际语言引起的,而不是由您编写的代码创建的任何错误引起的。 这可能令人沮丧。

The last point, which is more important to certain companies, is compatibility with C and C++ libraries. Being that Objective-C is a superset of C, it is easy to use C and C++ code with Objective-C. You can even use Objective-C++ if you feel so inclined. This is important if your are dependent on third party C and C++ libraries.

最后一点,对于某些公司来说更重要,是与C和C ++库的兼容性。 由于Objective-C是C的超集,因此很容易在Objective-C中使用C和C ++代码。 如果您愿意,甚至可以使用Objective-C ++。 如果您依赖第三方C和C ++库,则这一点很重要。

弱点 (The weaknesses)

The first main complaint I hear about Objective-C is syntax. I started my professional career using Objective-C so I have no issues with it. It is verbose and a bit unconventional with the use of square brackets. But opinions on syntax are just that, opinions. I figured I would list this point though since it is one of the first things that comes up when you mention Objective-C.

我听到的关于Objective-C的第一个主要抱怨是语法。 我使用Objective-C开始了我的职业生涯,因此没有任何问题。 使用方括号时它很冗长,有点不合常规。 但是关于语法的意见就是这样。 我想我会列出这一点,因为当您提到Objective-C时,这是第一件事。

One thing I do agree with though is that block syntax is frustrating. There is even a website dedicated to decoding the mysteries of blocks in Objective-C. I actually use this website pretty often as a reference.

我确实同意的一件事是,块语法令人沮丧。 甚至还有一个网站专门致力于在Objective-C中解码块的奥秘。 实际上,我经常使用该网站作为参考。

The biggest issue Objective-C faces right now is the fact that one day Apple may drop support for Objective-C with Cocoa and other common libraries used to create iOS apps. Since Objective-C is primarily used to create iOS apps, this would be a death knell for the language. It also means that newcomers to the iOS community are afraid to commit to learning Objective-C right now since it may no longer be used in the future.

目前,Objective-C面临的最大问题是,有一天,苹果可能会放弃Cocoa和其他用于创建iOS应用程序的公共库对Objective-C的支持。 由于Objective-C主要用于创建iOS应用,因此这是该语言的丧钟。 这也意味着,iOS社区的新手会害怕立即致力于学习Objective-C,因为它可能在将来不再使用。

Let’s get back to the language itself. It is prone to having hard to debug issues due to the dynamic nature of the language. The ability to send messages to nil and not crash on top of the lack of strict typing are some examples of things that lead to these hard to debug issues.

让我们回到语言本身。 由于该语言的动态特性,因此易于调试问题。 可以将消息发送为nil并且不会由于缺乏严格的键入而崩溃的能力是导致这些难以调试的问题的一些示例。

Objective-C does not hold your hand when it comes to these things either. Though it is nice that the app doesn’t crash when you send a message to nil, it may put your app in a weird state. It is very hard to debug issues like these. The fact that Swift has strict typing and the use of unwrapping optionals prevents these things at compile time.

在涉及到这些事情时,Objective-C也不会牵手。 虽然将消息发送到nil时应用程序不会崩溃很不错,但它可能会使您的应用程序处于怪异状态。 调试此类问题非常困难。 Swift具有严格的类型,并且使用了unwrapping可选选项,这一事实在编译时阻止了这些事情。

我应该学习Swift还是Objective-C? (Should I learn Swift or Objective-C?)

The answer for most people will be Swift. Apple is clearly pushing Swift as the language of choice for its iOS application development community. Swift will only continue to become more performant as ABI stability is introduced and Swift becomes packaged with the OS itself.

对于大多数人来说,答案将是Swift。 苹果显然正在将Swift作为其iOS应用程序开发社区的首选语言。 随着ABI稳定性的引入和Swift与OS本身的打包,Swift只会继续变得更加高性能。

If you’re looking to get a job as an iOS developer, Swift will be the language you want to learn. Most startup to mid-level companies will have their iOS apps written completely in Swift. This means you’ll be able to apply and interview for more jobs if you learn Swift.

如果您想找一份iOS开发人员的工作,Swift将是您想要学习的语言。 大多数初创公司到中级公司将完全使用Swift编写其iOS应用程序。 这意味着,如果您学习Swift,就可以申请并面试更多工作。

Even at larger companies where Objective-C is still used heavily, interviews can still be done in Swift. So you can learn Objective-C once you join the company and not worry about burdening yourself with more things to learn before the interview.

即使在仍大量使用Objective-C的大型公司中,也可以在Swift中进行采访。 因此,您在加入公司后就可以学习Objective-C,而不必担心在面试前会给自己负担更多要学习的东西。

You will want to learn Objective-C if you are already working at a start up or mid-level company and want to jump to a larger company. Skills with Objective-C will give you specialized knowledge and an edge over other interview candidates.

如果您已经在初创公司或中级公司工作,并且想要跳到一家更大的公司,则将需要学习Objective-C。 具有Objective-C的技能将为您提供专业知识,并且比其他面试候选人更胜一筹。

Liked what you read? Take a look at some of my other articles:

喜欢您阅读的内容吗? 看看我的其他一些文章:

Tips for your first tech interview.

首次技术面试的提示。

Starting a tech career from nothing.

从零开始的科技职业。

Should you get a computer science degree?

您应该获得计算机科学学位吗?

翻译自: https://www.freecodecamp.org/news/https-medium-com-colin-gabriel-smith-swift-vs-objective-c-5b19add8e2ed/

Swift与Objective-C:与恐龙有关的趋势相关推荐

  1. Swift调用Objective C的FrameWork

    很多Github的库经过很多年的发展,源码都是OC写的,,所以,用Swift调用OC的库就是开发中难免遇到的的一个问题,本文以AFNetworking为例,讲解如何跨语言调用. 第一步 创建一个空的工 ...

  2. Swift中的#pragma mark?

    本文翻译自:#pragma mark in Swift? In Objective C, I can use #pragma mark to mark sections of my code in t ...

  3. Objective C范型

    范型 范型编程是一种程序语言设计范式,它允许程序员在使用强类型的语言编写代码的时候,延迟确定具体的类型. 以Swift代码为例,假如有一个需求是要交换两个int,很容易写出类似代码 func swap ...

  4. Swift 代码调试核武-LLDB调试基础

    原创Blog,转载请注明出处 http://blog.csdn.net/hello_hwc?viewmode=list 我的stackoverflow 前言:LLDB是个开源的调试器,与XCode绑定 ...

  5. 让 Objective-C 框架与 Swift 友好共存的秘籍

    Python实战社群 Java实战社群 长按识别下方二维码,按需求添加 扫码关注添加客服 进Python社群▲ 扫码关注添加客服 进Java社群▲ 作者:江湖人称 "七叔" 其实就 ...

  6. GitHub上Swift开源项目!

    swift-open-project 这里汇集了目前为止最为流行的Swift开源项目,选取SwiftGuide中的开源部分,这里将每周对项目进行总结,如果大家有推荐的可以联系thinkloki@gma ...

  7. 粒子滤波 应用_如何使用NativeScript开发粒子物联网应用

    粒子滤波 应用 If you're developing any type of IoT product, inevitably you'll need some type of mobile app ...

  8. 【iOS与EV3混合机器人编程系列之中的一个】iOS要干嘛?EV3能够更酷!

    乐高Mindstorm EV3智能机器人(下面简称EV3) 自从在2013年的CES(Consumer Electronics Show美国消费电子展)上展出之后, 就吸引了全球广大机器人爱好者的眼球 ...

  9. Flutter 初学者的简单例子充分解释

    Flutter 初学者的简单例子充分解释 大家好,我是坚果,我的公众号"坚果前端", 关于 Flutter 语言: Flutter 是一种语言工具,允许您使用单一的原生编程语言代码 ...

最新文章

  1. 引入css的四种方式
  2. python将一个json字典转换为一个python对象
  3. 练习题3(求四位会员卡号之和)
  4. idea每次新建项目都要重新配置maven
  5. logback无法生成日志文件之谜
  6. 从前馈到反馈:解析循环神经网络(RNN)及其tricks
  7. ERROR streaming.StreamExecution:createConsumer(ConsumerStrategy.scala:63)
  8. 服务器双网卡设置安全_服务器的基础知识
  9. 19套最新的免费图标字体集
  10. Oracle中“行转列”的实现方式
  11. xx.net可能会出现的小bug(Ipv6篇)
  12. python怎么创建窗口_python如何设计窗口
  13. 微信小程序九宫格布局
  14. 数组方法中会更改原数组,不会更改原数组(详细)
  15. 被降级or地位凸显,技术研究院不同命运背后的企业技术底色
  16. redis三种连接方式
  17. 修改变量名,简单有效地提高代码质量!
  18. MODBUS-RTU数据帧格式、报文实例
  19. 仅改变rowspan背景_真实的他们《王者荣耀》全英雄历史故事以及设定背景介绍...
  20. 基于stm32f407vet开发板的人脸识别

热门文章

  1. qt4的quick程序升级到qt5_最新8月书单出炉!送给你程序员
  2. python下载电脑版本不对_初学Python,因为某些原因电脑只能装3.1版本,现遇到这个小问题求解答...
  3. 固定资产打印条码标签应用方案
  4. Laravel开启跨域的方法
  5. 为pony程序添加IACA标记(二)
  6. loadrunner另类玩法【测试帮日记公开课】
  7. RHEL6.3安装vsftpd
  8. IoC容器Autofac(1) -- 什么是IoC以及理解为什么要使用Ioc
  9. 《Two Dozen Short Lessons in Haskell》学习(八)- Function Types, Classes, and Polymorphism
  10. Q币才是腾讯真正的世界级产品