Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Swift Programming Language: Is it Better than Objective-C or What?

The iPhone developers earning billions bucks these days, and who are not willing to clinch bucks into their pocket? After some successful launches of an iPhone update like Health Kit, Cloud Kit , Home Kit and some more additional Extensions, iPhone have given a Big bang launch of Swift, that will be bliss all the developers: “A brand new programming language for an iPhone application development.”

I know that everyone is inquisitive to get in touch to Swift Programming Language. I have written this article to end your inquisitiveness and give an overview of a Swift programming language.

Swift and objective-C are Analogous or Not?

The answer of the above question is YES, You will see the Same API which objective-C uses. Experts of objective-c would be pleased to know this as they can do anything with Swift which they could do with Objective-C. It means you will get everything what you had seen in Objective-C with some additional new concepts that you will find in this article.

Well, below I have covered some aspects of the Swift. A beginner of iPhone application development can look upon it and a developer of an iPhone application can compare it with an Objective-C.

Viewpoint of Swift

Here are the points that have been introduced to swift, you can say as an improvement to Objective – C which are as below.

Code Initialization: Initialization of objects and variables must be done at the time of definition. If you have forgotten object or variable to be initialized, you will get compile time error in Swift. This will ensure that an object or variable always has a value.
If there is a case when you can’t define an initial value to the object or variable, this type of variables and objects are known as Optional in the Swift programming.

If Branch completeness: All conditional branches must cover all the conditions, this way no code will remain uncovered. If there is any missing condition in any statement, then the compiler will generate compile time error in Swift.

Constants and Variable definition:
Apple has introduced a new keyword to define constant in the swift programming and have encouraged programmer to enhance the use of constant variables in programming to make your programming code safer. This will help with code optimization as well because when you define a constant it means compiler knows that constant value never change.

let someConstant : String = “This is a constant”
var someVariable : String = “This is a variable”

As you can see in the above example, let keyword is used to define a constant variable whilst var keyword used to define a variable.

What other improvements you will see in Swift?

As you know now that Swift language have improved all the issues that had founded in Objective-C, C,C++.

  • Out of bound indexes in arryas
  • Return types : unchecked
  • Poiinter access: unchecked
  • Uninitialized data
  • Errors : goto
  • Implicit fall through

Say bye to Header files (.h) and implementation files (.m):

In objective-C you need to add header files and implementation files, but in a Swift programming language you will not need to bother for such an importing of files. You just need a single implementation file (.swift) that contains all the class of implementations and definitions.

Say bye to semicolons:

In Swift programming language, there is no need to add a semicolon at the end of each line to terminate the statement because in Swift each line is considered as statement line. Well, this is confusing for me that how you all going to take it. Because semicolon is a syntax that we have been seeing since C programming language to the objective-c, java, asp.net or any you can take which is now ceasing by Swift. Yes, that is true that you will face interruption of adding a needless semicolon due to habit.

Say Bye to Heartbleed bugs:

In Objective-C curly braces of if statement was optional which was creating bugs called Heartbleed. To get rid of these bugs Swift has made curly braces mandatory for each and every if statement.

Changes that Objective-C developers will notice in Swift Programming:

Here are code snippets that show you similarities between Swift and Objective-C Programming language.

Objective-C Programming Swift Programming
// Objective-C
for (int index = 0; index < 5; i++) {
NSLog(@”%d”,index);
}
// Swift
for index in 1.. plrintln(“\(index)”);
}
// Objective-C
switch(index) {
case 0:
break;
case 1:
break;
default:
break;
}
// Swift
switch(index) {
case 0:case 1:default:
}// no break statement
// Objective-C
if (index == 0) {}
// Swift
if index == 0 {}
// parentheses are optional
// curly braces are required

In the above example, you can see the highlighted syntax which is something new in Swift programming language that is known as range operators. There are two types of range operators that Swift have introduced.

  • 1. ..< : Half closed range operator specifies a range of values, for an e.g. ..
  • 2. … : Close range operator specifies a specific range of values, for an e.g. 1…5 which includes 1,2,3,4,5 .

Variables and Constants

Again, I have taken same example which we have seen above to give you some elaborated description about variable and constants.
let someConstant : String = “This is a constant”;
var someVariable : String = “This is a variable”;

In the above example we have defined constants using let keyword and variable using var keyword and the colon: sign to define the types of the variable. This above example is an example of type String. You can see there is not any @ symbol in the initialization of a string variable or constant. It is just like a C or Objective-C.

So what is the difference between Objective C and Swift in this Prospective?

Well, the answer is compiler of swift assumes the type of the variable itself, unlikely to Objective-C and it also ensures that not even a single incorrect casting of variables occurs without your interference. So let’s rewrite the above example and let Swift compiler intervene.
let someConstant = “This is a constant”;
var someVariable = “This is a variable”;
let someInt = 1;
let someFloat = 1.0;

In above example compiler will recognize itself that someInt holds an integer value and someFloat holds float value. Isn’t it great in look as well in employment!!

In above example compiler will recognize itself that someInt holds an integer value and someFloat holds float value. Isn’t it great in look as well in employment!!

Strings

If you want to assess the strength of any language, then should check how it manipulates string operations. Shall we undergo with swift also? Let’s do it.

Objective-C has the below format of declaring and initializing string, which is much better than other languages. But, as time last, it got more confusing. Now look at the Swift example, you will find an easy way of string concatenation.

Objective-C Swift
NSString *string = @”Hello “;
NSString *greeting = [string stringByAppendingString:@”World!”];
let string = “Hello ”
let greeting = string + “World!”

Besides that strings are Unicode in Swift, it means you can use it in any language like.
let string = “你好世界”
You can create a string using iteration for. .in as below

Code Output
let str = “Hello”; // outputs
for char in str {
println(char);
}
// H
// e
// l
// l
// o

String Interpolation:

In Objective-C, string Interpolation is done by invoking [NSString stringWithFormat:]. Whereas, using Swift you can do it with ease as follows. What you need to do is just wrapping the variable or function call in parentheses and add the backslash in front of variable or function call. E.g. \ (Expression).
let x = 4;
let y = 5;

println( “\(x) x \(y) = \(x*y)”)

// outputs
// 4 x 5 = 20

Array and Dictionary in Swift and Objective-C:

Similar to Objective-C, Swift also have a set of classes and adds which are known as Array and dictionary having similar declaring syntax as Objective-C including [] and excluding @ at the beginning of [].

let someArray:[String] = [“one”,”two”,”three”];
var someOtherArray:[String] = [“alpha”,”beta”,”gamma”];

Same applies for Dictionaries:

let someDictionary:[String:Int] = [“one”:1, “two”:2, “three”:3];
var someOtherDictionary:[String:Int] = [“cats”:1, “dogs”:4, “mice”:3];

Wrap up:

Now, I wish you have a clear view of Swift programming language. From my prospective Swift is quite easier language for developing as well as for understanding. It is a simply an Improved version of Objective-C for an iPhone application development. So what are you waiting for, Get started to study it.

The post Swift Programming Language: Is it Better than Objective-C or What? appeared first on TOPS Technologies Blog | News and Updates.



This post first appeared on TOPS Technologies Blog | News And Updates |, please read the originial post: here

Share the post

Swift Programming Language: Is it Better than Objective-C or What?

×

Subscribe to Tops Technologies Blog | News And Updates |

Get updates delivered right to your inbox!

Thank you for your subscription

×