New Lang from Apple, Swift – Making a HTTP Request

Bye bye Objective-C, welcome Swift!

Today, Apple released its new language which claims to lose C from ‘Objective-C’ and much more. I downloaded Xcode 6 Beta and tried to implement a simple HTTP Request, after reading some of their very good-written documentation about Swift. For brief features here, for iBook version here.

My first impression is, it seems like a good language, however popularity of this language will be increase not only by that, but also the great market. The reason why Objective-C is on Top 3 in TIOBE Index, my personal opinion is because of that.

Update: Well, After implementing Socivy and migrating Dilixiri to Swift, “good” seems to be not enough, for a very young language, it’s perfect. (even though, there are some small problems with UISearchBar with Scope Bar, that’s ok 🙂 )

(Apple may be inspired by Go-Lang for the type annotations.)
(Check Swift REPL mode, you will like Playground feature.)

Swift syntax seems much clear while comparing with the Objective-C version of the code above, despite its cleanness, I believe some of the core Objective-C libraries must be rewritten.

As you can see above, for making an HTTP request I have used old NSURLConnection class, and called a very sendAsynchronousRequest method. For making libraries both accessible from Objective-C and Swift, seems like Apple intention currently, so I am not expecting a huge API changes on core libraries such as UIKit etc. Apart from that, there will be dozens of new third-party libraries which will be more swiftic(like pythonic) than Objective-C ones. (For instance, HTTP library for python, requests is more pythonic than standard python library for making http requests)

For projects that are awaiting me on iOS platform this summer, it is exciting yet, a bit sad to put Objective-C behind. And yes, I think I am going to go with Swift for these projects. 🙂

Small note: My async handler unables to access to the UI, for some reason I don’t know(weirdly sometimes does). I tried to implement it by looking some of my old Objective-C implementations. Since there is nothing on the web about Swift now. (It is released just today, come on!) If you know why, it would be great if you write a comment.

Update: This is because of GCD. Please check this detailed explanation.

Tags: , , ,

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.