Dec 1, 2014

Why I think Swift is not ready yet




UPDATE Feb '15: With Xcode 6.3, Apple fixed some of the issues this post is talking about. However - there are still some major issues for me, the number one of them is the fact I can't use Swift classes in Objective-C without porting the tests for them to Swift.

So,
Like every relatively-early-adpoter-iOS-developer, I decided to give Swift a try in the past few features I worked on for Piano Maestro.

It was a lot of fun to get into, and I wrote some pretty code and features came out very nice. However, I ran into some issues, that made me eventually come to the conclusion that swift isn't quite ready yet, and I should stick to Objective-C as the main language for developing iOS apps. At least for now.

Here are my top reasons:

Inaccuracies in compilation errors
Often, when a line is relatively complex and has a compilation error, compiler will give you an error message that's simply not describing the real issue.

The most common use case is a simple error like sending the wrong primitive type as an argument to a function (a problem that happens a lot to us Objective-C/C/C++ developers, which are used to implicit conversion of primitives).

Consider the following example:
WAT??
dispatch_after receives exactly these types arguments. So what's wrong here?

Turns out the problem is that dispatch_time expects Int64 to be sent as the second argument, and I actually sent NSTimeInterval.
But how could I know that from that irrelevant error message?

There went an hour of banging my head until I figured this out.


UPDATE Feb '15: at least for this example, was fixed in Xcode 6.3 Beta 1 and error message is now very descriptive and relevant.




Debugging is hard
More head banging often happened when trying to see the value of a variable in the debugger, I eventually find myself adding debug prints instead.
A tip about this: If you get "Some" as the output of trying to 'po var', you can 'po print(var)' instead, and it will (hopefully) work.


Can't use Swift code within Objective-C in unit tests
Update May 2015: I found workaround!

While it's absolutely possible to mix Swift <-> Objective-C code in the app's main target (apart for some limitations I will rant about later), in the test target, I can't import $(PRODUCT_MODULE_NAME)-Swift.h in Objective-C code.
This does not only mean that tests for Swift classes can only be written in Swift (fair enough), it also means that any Objective-C code in my app that uses Swift classes cannot be tested at all. Build will fail if it's included in the test target of the project.
LAME.

See:

Can't use tuples and trivial enums in Objective-C
As the official documentation says, you can't use Swift-only features in your Objective-C code.
This makes sense for some things, but it would make a huge difference if they could have made an effort to expose some of the more trivial features in the $(PRODUCT_MODULE_NAME)-Swift.h bridge:
- Trivial enums can become C enums with a prefix (like it's done in the opposite direction)
- Tuples can become trivial classes
etc.
For now, I can't expose functions that receive a tuple or a Swift enum to Objective-C, and must add some kind of bridge myself (e.g. get an Int parameter instead of an enum and use the constructor with rawValue:). 
Maybe it was too much of an effort for Apple to deal with, but this makes it harder to use these features if I know the class will be used in Objective-C code.

UPDATE Feb '15: you can now use trivial enums!

Can't use c++ in Swift
If like us, your app uses Objective-C++ code and not only Objective-C, you're in trouble and won't be able to use any code that uses c++ types in the header. You'll have to create some kind of Objective-C bridge that will hide the c++ types.


Can't use mocking frameworks
Because of limitations in the availability of the language runtime in Swift, there's no good way to use a mocking framework for tests right now. That's a big problem.
In the meanwhile - best workaround IMO is adding an Inner Class called "MockMyClass" in your test, and overriding any methods you want to mock (like we did in 2006 before mocking frameworks were introduced).

See:

Xcode sucks, AppCode has only limited support
So, as you probably know from my Xcode vs AppCode comparison (a bit outdated, but most of it still applies), I don't use Xcode for coding. It's lacking some very basic IDE mechanisms that AppCode is excellent at (e.g. Extract Method, Quick Fix, Find Usages, ...).
This is still true for Xcode 6.1 and Swift support. Sure, Xcode improved since writing this comparison, but still - I can't even extract method? This sucks.
Unfortunately, Swift support in AppCode is still very rudimentary. But the good news is that they are working on it.
In the meanwhile, here's a link to AppCode EAP that contains some basic features of syntax highlighting and some basic refactoring in Swift. I still don't recommend it over Xcode's current Swift support though.

UPDATE Feb '15: AppCode 3.x has basic Swift support, but still has a very long way to go in order to be as good as it is for Objective-C in terms of refactoring etc.

To sum it up

I guess things are not that bad, because Swift is a very neat language and has some really cool features I'm in love with, like tuples, better enums, cooler closures, generics, property observers, and many many more. 
I assume my complaints are either going to be solved soon, or become less of an issue - many of them are mix&match issues, which only affect apps with existing Objective-C/C++ code. Hopefully

Anyhow, at least for the time being, I guess I'll stick with Objective-C as the main iOS development language.

Further reading:
Developing a Bidding Kiosk for iOS in Swift - a great retrospective with some more reasons of why Swift isn't ready yet.

18 comments:

  1. Yep, it's a worthy addition to Objective C but out of the question for my use because of productivity issues. Apple incorrectly introduced Swift as the replacement for Objective C at WWDC2014, which was a mistake given the state it's in (it should have been introduced as an additional language). As of today (Feb 10), they released a 'migrator' to convert 1.1 projects to 1.2. That tells you everything you need to know. It's also more difficult to code review than Obj C (at least for me).

    ReplyDelete
  2. I am an experienced C++/C/Obj-C/Ruby/Fortran programmer. When I write Swift I feel I have to understand most of Objective-C just to figure out the 'why' behind all the exclamation marks, etc.

    Then the app I built runs fine in debug, corrupts strings in release, and I'm pretty sure its a compiler bug. In C its easy to make bugs that only show with optimization on, but in a language like swift are we not protected from most of that?

    It feels complicated.

    ReplyDelete
    Replies
    1. If you get strings corruption you are doing something seriously wrong.

      Delete
  3. wow, These are exactly the issues I am facing and google lead me to your page :) .

    ReplyDelete
  4. This site has lots of advantage.I found many interesting things from this site. It helps me in many ways.Thanks for posting this again.Further details please visit our website..
    Oracle Fusion Training Institute

    ReplyDelete
  5. individuals at the top, who are as yet carrying on that excellent practice of being enamored with film. In the event that you are incredibly energetic about being in this business and selling a content, at that point you need.

    ReplyDelete
  6. Offer the workforce time to get flu chances. This should be paid time away from work to encourage them to get shots during flu season. Can Someone Do My Essay For Me, Offering Free Flu Vaccination at Workplace

    ReplyDelete
  7. Nonetheless, learning at the home offers interruptions like tasks, tasks, phone, family duties. Hence, understudies who take an interest in distance learning should act naturally train. For example, to try not to be enticed by online interruptions, understudies can sign out of email and web-based media while doing schoolwork. To evade disconnected interruptions in addition to common interruptions, you can go to a different room or set a particular timeframe that is carefully devoted to finishing the timetable.

    ReplyDelete
  8. On the off chance that you have infants or more seasoned individuals in your home, it is of most extreme significance that the climate remains clean and residue free. They are more vulnerable to creating invulnerable insufficiencies or breathing issues. The solitary way you can guarantee the air remains perfect and new is by having a private cleaning office consistently play out their administrations in your home.

    ReplyDelete
  9. Their assignment writers across. Homework Services the globe are well trained in their chosen field of study which reflects in multiple offer. It is important to choose the best one among the many.

    ReplyDelete
  10. Alaqsa Store offers largest variety of Alaqsa Store fresh halal meat near me to your doorstep. Now offering free shipping all over US.

    ReplyDelete
  11. Our doctors has a personable and intimate rapport with her patients throughout their inner and outer health journey. She has enjoyed counseling patients and has addressed patients beyond the scope of traditional gynecology. In addition, Our doctors for men in addressing their hormonal imbalances.

    ReplyDelete
  12. Who Are the Best Lawyers in area of Philadelphia and New Jersey History? Search Philadelphia Best Lawyers® Best Philadelphia Law Firms rankings for firms near you by using our website search bar.

    ReplyDelete
  13. Our headquarter is in Philadelphia, PA. We have a global existence, operating in the USA, UK, Asia, and Australia. We are serving our clients all over the globe. Regardless of different time zones, our team manages to facilitate and collaborate with them to deliver high-quality services.

    ReplyDelete
  14. The goal of Williamson Caterers is to meet and exceed our clients’ expectations and make certain that our clients’ enjoy their event as much as their guests.

    ReplyDelete
  15. We are the leading provider of quality tires and exceptional services in New Castle for 25 years. We have 2 service centers that offer a range of services from an oil change, tire replacement to everything your vehicle needs, and delight our customers.

    ReplyDelete
  16. Dr. Ralph’s Automotive Services was established in 1976 by Ralph Sposato and is operated by himself and his son, Franco. Ralph’s passion for cars and automotive technology started at a young age. He was determined to be a great mechanic.

    ReplyDelete