Swift or Objective-C Which One Shall I Choose?

On December 14, 2015, in iPhone iPad Object-C, Swift, by James Liu

As an Apple developer, Swift or Objective-C, which language shall I choose? This is a very good question. Even as an experienced apple developer, it is always hard to decide. As the first time, Apple announced swift language at 2014. And now it is much stronger. In 2015, Swift is open source. Either you are an experienced apple developer who are expert on Objective-C, or you are a beginner who want to learn programme on Apple platform, it is time to watch on Swift. As I always say, the best lesson for learning a new skill is doing something real. Therefore, I decide to write my next ios Apple by Swift. Before I release my first Swift project tutorial on my website, I will write down some basic learning points which I have learned from my first Swift project.

My first Swift tutorial is released: Get People Name and Phone Number from Contact by Swift

Declare Constant and Variable

Declare Constant and variable is much simpler than ever in Swift. To make a variable, we can just following code (like Javascript):

var myVar = "Hello World!";

How to Declare Variable with nil

Even swift provides a simple way to declare variables. But it also introduce a strange way to declare null object. (For me, it is quite strange at least!)

//wrong way
var myVar;

Above code will give a “Type Annotation missing in pattern” error. Then, what’s the way to declare a null object?

//wrong way
var myVar = nil;

Above example code still gives an error “Type of expression is ambiguous without more context“;

Then, what’s the right way, seriously? Swift introduce an Optional Types for properties. Here it is:

//right way
var myVar:NSString?;

Then, what’s the value of following variable?

var myVar:NSString;

OK. We declare this variable with NSString type. Different from Objective-C, we cannot use following code to assign a null value.

var myVar:NSString;
myVar = nil;

This will give the error message “Nil cannot be assigned to type “NSString”“. It seems that only optional type variable can be assigned nil value. The myVar in above is a NSString object with not being initialized. I don’t know what the value is. As I know, when you are using not initialized object, Swift will give an error “Variable myVar used before being initialized”

Unwrap an Optional Type

Once we declare an optional type variable, then we need to unwrap that variable before we can access that variable methods. So we need to unwrap the optional variable by exclamation mark “!”, or we will get error “Value of optional type ‘NSString?’ not unwrapped; did you mean to use ‘!’ or ‘?'”.

var myVar:NSString? = "I am James";

Define Class

In Swift, class definition is very easy. We don’t need .h file any more. Here is an example to define a class with an class variable.

class MyClass {
    var myVar:String?;

    func hello() {
        this.myVar = "Hello World";

Leave a Reply

WordPress Themes

Premium WordPress Themes