Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Mobile / iOS

Swift or Objective-C? that is here the Question!

5.00/5 (1 vote)
6 Oct 2014CPOL1 min read 9.8K  
Some toughts whether to prefer Objective-C or Swift.

Introduction

These days in the Apple universe often is asked whether to start in the new language Swift or Objective-C.

Background

I am programming in Objective-C but now diving into Swift and have some experienve and see some pros and cons.

Where is the ";"?

The answer is as expected: 42. These languages arent so different. They are both translated with the LLVM, but Swift has more and better optimization. Also is "bridging" between these languages possible. Swift is in many way a "compressed" version of Objective-C. 
 
For a starter I would recommand Objective-C because it is the common language and a lot of code is available.

Swift is more modern and very compact, so it need REALLY no semicolon and in few lines of code much stuff is done. For instance it has no header file and declarations also are defining the type and that the value is constant.

let text = "I am a fixed text" // let declares a constant
var number = 10.0 // var declares a variable, the right assigment declares the type double
var state = eStateNormal // a var of my EnumState

Remark: It is also possible to explicitly define the type, but because the needed initialization it is not needed.

var text:String = "I am a normal text" // declares a normal string

Tricky gets Swift at optionals. This are "boxed" types which may or may not hold a value. Because they throw exceptions coders should take care in handling them.

var object? //needs no value now

object = "Hello world" //assigment

if let text = object! { //unwrapping the optional
  println("Value is \(value)")  //how to format a string
}

Points of Interest

It is my short hand point of view and dont want to make it a battleground. 

For detailed information is the first point the Apple Developer Site. I like the WWDC videos to get started.

Also I recommand to take a look at the website of the famous Ray Wenderlich. For a starter like you is the Video plan an interesting investion.

I would start with Swift only on a new "blank sheet" project, because I dont like switching the languages in one project.

History

First shot.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)