Moving Swiftly

Moving Swiftly(从OC到swift)Swift1.0 not funTrouble1.source compatibility代码兼容性2.swift->Xcode->iOS 版本依赖Salt in the wound伤口上撒盐error reporting 错误提示错误提示改进swift2.0Think about the good times好处呢?

Moving Swiftly(从OC到swift)

31 Aug 2015

Last year, Apple revealed Swift, the future of software development on their platforms. Next Wednesday they’re expected to officially release Swift 2 as part of Xcode 7 GM. In just a year, everything has changed. We went from writing our apps in an object-oriented flavour of C, to writing almost exclusively Swift. I’m personally writing more code than I have in years, in part because it’s just fun.

Swift1.0 not fun

Now, to be clear, writing Swift 1.0 was not fun. It was novel, it was at times exciting, but it was not fun. The compiler would crash, the syntax highlighter would crash, the IDE would crash. The running app would surprisingly not crash, but that was pretty much the only non-crashing thing in the vicinity of any Swift 1.0 developer’s desk. I think this caused a bit of bad blood, and a number of folks who tested the Swift waters early promptly returned to the Objective-C they knew and loved.

While that was happening, Steamclock dug in. We started building internal tools with Swift last June. That October, we decided to start giving clients the option of going for Swift for new app builds. It seemed optimistic, but we were excited to level up our Swift experience and give new projects a leg up on the future. The results were blow-away: of roughly a dozen clients, only one opted for Objective-C. (这是爱不释手了吗?)Swift became our default programming language almost overnight.

Now, as the language is hitting its stride, we’re more productive, we’re seeing fewer issues in the field, and our clients are happy to be ahead of the curve. Almost as importantly, it’s fun - it sure makes recruiting easier when you’re hiring people to write Stack Overflow’s “most loved” language. (By the way, if you’re a Vancouver mobile developer who’d like to come write Swift at Steamclock, get in touch.)

Trouble

1.source compatibility代码兼容性

That’s not to say that the Swift transition didn’t cause some pain. For example, the explicit lack of source compatibility between versions is pants-on-head insane. At least, that’s what people say when they hear the term “no source compatibility between versions.”

In practice it’s not quite so bad. Lack of source compatibility means that each time a new version of Swift is released, you need to block out an hour to a day to migrate your code to it, after which point it will no longer run on the old version of Swift. In the common case that you’re not distributing your code that’s not so bad, but when you start dealing with Swift-based CocoaPods, it gets messy.

2.swift->Xcode->iOS 版本依赖

The bigger issue is that Swift versions are tied to Xcode versions, and Xcode versions are tied to iOS versions. So, if you want to debug your Swift app on an iOS 9 device, you need to use Xcode 7 beta, which only compiles Swift 2, which means you need to port your app to Swift 2 to debug it on iOS 9. Meanwhile, even this late in the game, you still can’t send betas to customers or reviewers from Xcode 7 beta, which means you can’t beta test Swift 2 apps, which means Testflight and Swift don’t go well together.

It’s clear they’re going to improve this little mess, though it’s unclear exactly when or how. Either Swift needs to start locking down some source compatibility, or Xcode and Swift need to be more decoupled. Either way, Testflight definitely needs to support external beta testing with beta versions of Xcode, because srsly.

All that said, we’ve successfully worked around it, mostly by relying less on Testflight and by avoiding the Xcode betas for projects that might need to be distributed to the App Store any time soon. It’s not ideal, but this dance been a worthwhile tradeoff for the benefits Swift gives us. It seems like the language is in a good enough place that future versions should require substantially less thrash, but are we out of the woods yet? It’s a bit hard to tell.

Salt in the wound伤口上撒盐

error reporting 错误提示

With Swift 2, the other issue I still regularly hit is quirky error reporting. Errors matter a lot in statically compiled languages, which ask a lot more of developers than a loose language does in terms of just getting the damn thing to run. Swift is more heavy-handed this way than Objective-C, due to its stricter mutability and optionality rules.

As such, writing Swift code that doesn’t compile is easy, especially when you’re learning. Luckily, a great compiler can have a nuanced enough understanding of your code to point out issues clearly and rapidly. Swift was designed to enable exactly this, via great autocompletion and expressive error messages.

Unfortunately, while the infrastructure for developing good errors was in place early on, initial versions of Swift tended to produce very misleading errors, even from simple typos. Even with 2.0, Xcode will give you the occasional self-referential error that really makes you wonder what exact strain of hallucinogen it has recently consumed.

错误提示改进swift2.0

Luckily, the errors are getting a lot better, and seem to be rightly recognized as a priority to the Xcode team. In many cases, Swift now gives better errors than were possible in Objective-C. For example, this code can’t compile because Swift won’t coerce Int to a String:

let number: Int = 1

let text: String = (number < 0) ? "" : number

For that code, in Swift 1.2 the error you would get was:

Could not find an overload for < that accepts the supplied arguments

Which is wacky, since “<” has nothing to do with the problem. I filed a Radar, and in Swift 2 the error is now much better:

Result values in ‘?:’ expression have mismatching types ‘String’ and ‘Int’

Which is exactly the problem and immediately clear. With that positive reinforcement, I and many others keep filing silly error Radars. The error messages keep getting better, and my affection for Swift grows.

In this way, the march of progress has been both steady and relentless. Swift 1.1 brought improvements to the Cocoa APIs to make them more Swift-friendly, tweaked and improved the language, and made Xcode more reliable and performant. Swift 1.2 brought more of the same. Swift 2.0 brought yet more. A year later, the annoyances and remaining issues feel small compared to benefits we’re getting, and it just keeps getting better.

Think about the good times好处呢?

Meanwhile, lot of smart people are still holding off on using Swift. I get the rationale – it was rough out of the gate, and learning a new language is a substantial undertaking. Swift is a bigger language than Objective-C or something like JavaScript, and it has a lot of depth. This is clear from the fact that other smart people are still theorizing and dissecting their experience of learning Swift, working to understand how best to use this new tool.

Yet as much as it may seem like work, this investment will pay off quickly. Basic Swift is very easy to write, and learning something new never goes out of style.

So I have a proposal to all you Objective-C developers out there. Next Wednesday, when the Xcode 7 GM delivers Swift 2 in its full shininess and, likely, open source goodness, jump in. Start something new. A side project, a game, a new view in your existing app - anything to get over the idea that you don’t know Swift. Something to get you addicted. Know that the fear of something big and new, the fear of losing what you know, the fear of “doing it wrong” at first, is natural. You’ve just gotta shake it off. Shake it off.

转载于:https://www.cnblogs.com/xilifeng/p/4778665.html

Moving Swiftly(从OC切换到Swift)相关推荐

  1. iOS 中 OC项目使用swift第三方工具(OC、swift 混合)

    OC项目引入swift, 1.建立 bridge 文件:在OC工程中新建swift文件,确认创建时,会提示建立 一个 项目名-bridging-header.h 文件. 2,建立 bridge 文件之 ...

  2. 记OC迁移至swift中笔记20tips

    写久了OC后来写swift,总感觉写着是swift的皮毛,但是实际上是OC的核心,这里整理了OC迁移至swift中的一些小细节. 1 在当前类中,实例方法调用属性以及方法都可以将self省略掉,而且是 ...

  3. swift项目调用OC库 和OC项目 在swift文件里面全局调用OC库

    最近比较清闲,就把以前学习的过程记录下吧,多少年后如果能在互联网上找到自己的痕迹,想想还是一件蛮值得高兴的事情. 正好这两天加了一个swift学习群想继续学习swift,发现好多swift初学者,在混 ...

  4. OC中调用Swift代码方法

    最近在项目中使用到了Swift语言写的第三方的框架,所以在这里将OC中使用Swift的方法说明一下. 1.首先要在Build Setting中进行相关配置 2.在Podfile文件中要加上use_fr ...

  5. oc 项目倒入swift 代码注意事项

    oc 项目倒入swift 代码的时候 pod 引入库,必须使用user_frameworks 新建swift 类,会自动创建 xxx-bridging-header,点击创建就ok 如果swift p ...

  6. OC类导入Swift工程演示

    2019独角兽企业重金招聘Python工程师标准>>> 1.首先建一个Swift工程 2.新建一个OC类 3.这时,会弹出一个对话框,选"是" 4.会自动创建一个 ...

  7. Swift项目中不能定义OC类继承Swift类

    2019独角兽企业重金招聘Python工程师标准>>> 报错原因 Swift项目中 OC文件类  继承  Swift文件类 Cannot subclass a class with ...

  8. swift项目调用OC代码,OC项目调用swift代码

    swift调用oc 在一个 Swift 开发的工程中可以通过两种方法来生成桥接文件. 第一种是,直接拖拽 OC 文件到工程中,此时系统会询问是否生成桥接文件,如图: 选择 "Create B ...

  9. OC代码转Swift代码

    初始化函数 对于oc中的[[XX alloc]init] swift不需要alloc 也不需要init.直接调用以类名为函数名的工厂方法进行初始化. 声明的变量可以不显式声明类型 //objectiv ...

最新文章

  1. pythonassertbug_还在 Bug 不断?不妨试试这 2 个装X技巧
  2. 多个微服务控制台的多窗口展示
  3. 详细程序注解学OpenCL一 环境配置和入门程序
  4. 《C++ Primer Plus 6th》读书笔记 - 第8章 函数探幽
  5. C#中[WebMethod]的用法,aspx、ashx、asmx
  6. 外企面试官们爱提的十个问题
  7. 无处不在的container_of
  8. 2019年技术盘点云数据库篇(一):UCloud专家谈云数据库:千锤百炼 云之重器
  9. 15个SaaS问答(图文并茂版)
  10. HDP直播个性化设置教程,简单几步提升电视盒子/投影仪观影体验
  11. 无损图片压缩-设计师程序员的心头爱
  12. 经常使用的几种OCR文档扫描工具|无水印|避免智商税
  13. 灰色预测的MATLAB代码
  14. 腾讯/网易/263/阿里云企业邮箱登录入口
  15. 移动端 web 开发的设计稿与工作流
  16. No qualifying bean of type ‘service‘ available:单元测试报错
  17. vue 子组件调用($emit)父组件方法后父组件方法如何回调子组件方法
  18. 【洛谷】P1567 统计天数
  19. 校园网拨号上网一直掉线但实际网络通路是连接着的
  20. mac时间机器的使用

热门文章

  1. xUtils3入门使用及部分解析,androidstudio物联网
  2. android xutils json请求,Android Xutils3网络请求的封装详解及实例代码
  3. pyecharts 显示坐标轴线_实战PyQt5: 153-QChart图表之对数坐标轴
  4. 从公式到代码理解opencv和基于opencv实现的fastest pattern mathing的关系
  5. echarts 饼图label换行显示
  6. 删除的视频怎么找回来呢?
  7. 大一课设ktv点歌系统c语言
  8. 【C语言练习——交换两个变量的值】
  9. Linux配置Oracle环境变量
  10. 案例:简单版发布和删除留言案例