03
Jun

Apple’s Swift for iOS

Swift was introduced on WWDC by Apple as a new programming language for iOS and OS X. Apple claims that it’s much faster than Objective-C and has better syntax but let’s get deeper into to to judge ourselves. Official apple note:

Swift is an innovative new programming language for Cocoa and Cocoa Touch. Writing code is interactive and fun, the syntax is concise yet expressive, and apps run lightning-fast. Swift is ready for your next iOS and OS X project — or for addition into your current app — because Swift code works side-by-side with Objective-C.

Now we all have to download the Apple’s book and dive into it. Below I present some of my first impressions.

Some advantages we can see right now:

  • one line of code could work as a complete program, no imports, no main method
  • constant value (made by keyword let) doesn’t need to be set at compile time
  • switch support any kind of data (aren’t limited to integers)
  • switch cases doesn’t need the break keyword and got a lot of more power
  • multiple return values in functions
  • methods inside enums and structures – this could help is some complicated scenarios
  • generic forms of functions, methods, classes, enumerations and structures
  • Tuples – group multiple values into a single compound value, like
let http404Error =  (404, "Not Found")

Other helpful things that I could live without:

  • no semicolons at the end of the statement (ok, but I’ve already got use to do that ;))
  • nesting functions
  • nested multiline comments
  • range operator used in for loops
// contains from 1 to 10 (including last value)

for index in 1...10

// contains from 1 to 9 - half-closed range operator

for index in 1..10

Disadvantages:

  • Bool is now true / false
  • no prefixes for types (means if you type ‘s’ then you will get all the autocompletion options with types and other stuff)
  • function declaration lost its magic, now looks like this
func sampleMethed(arg1: String, arg2: String) -> (String, Double)

  • simpler way to include values in strings:
let testNo = 5
let stringTest = "This is my (testNo) test."

I liked the old way better

  • omitting ‘@’ sign when creating arrays, dictionaries, etc..  – I kinda got use to it and liked it, it made non Objective-C developers scared 😉
  • new empty dictionary syntax
let newDict = Dictionary<String, Double>()

let newDict2 = [:]

Noooooooooo…. This is like .NET!!!! Why are you doing this to me!?

  • in if statement, the conditional must be a bool expression, this won’t work
var newString:String = "aaa"

if newString {
    println("OK!")
}

This will

var newString:String = "aaa"

if newString != nil {
    println("OK!")
}

// OR
// using optional value

var optionalValue: String? = "aaa"
var result: String = ""

if let stringVal = optionalValue {
    result = stringVal
}

println(result)

Overall, maybe it’s new and hip but I don’t think I am gonna like it. I felt like playing in a good game while developing so far so we will see how the experience for a developer will change. Hope that it will not to as bad and I will not end up writing in old (soon probably unsupported) Objective-C and being uncool. Still there is much to learn.

Comments ( 0 )

    Leave A Comment

    Your email address will not be published. Required fields are marked *