Objective C to swift
How to Migrate Objective C to Swift
Objective C to Swift |
The main purpose of introducing the Swift is to deliver a simpler, new easier, more flexible programming language in order to code than Objective-C. As of now, Swift is 3.0 is the latest version of the Swift. And the Swift was announced at WWDC 2014. As the time evolved, the Swift slowly but with precision started to take over the main stream position for the iOS app development.
Some companies are continuing using the Objective-C in case, their main features are written in Objective-C which is a good decision as of the fact that mixing two languages will expand build size and there is an perennial advantage of C++ integration of the language.
Swift in objective C is an important topic and every enthusiasts of the web development world should know that. This was basically evolved to enhance the field of IOS world and the development in Macs. Let’s learn about everything that is related to the objective C to swift and all the terminologies that are involved in it.
Difference in Basics
Print
There are no more NSLog. The programmer can
basically use the print function and can also use the variables in
strings directly. Here is an example that is depicting the same:
let greeting = "Guten Morgen!"
let myName = "Yusuke"
print("\(greeting) Mine name ist \(myName))")
let myName = "Yusuke"
print("\(greeting) Mine name ist \(myName))")
· Strings
Handling
strings is very much simpler than the Objective C as compared to the Swift. In
case, the programmer wants to append strings with Objective-C, the it will look
something like the code that has been depicted below:
NSString
*name = @"Swift";
NSString *greeting = [@"Hi! " stringByAppendingString:[NSString stringWithFormat:@"This is %@!", name];
NSLog(@"%@", greeting); // > "Hi! This is Swift!"
NSString *greeting = [@"Hi! " stringByAppendingString:[NSString stringWithFormat:@"This is %@!", name];
NSLog(@"%@", greeting); // > "Hi! This is Swift!"
Now let’s
see what it will look like with the swift:
let
name = "Swift"
var greeting = "Hi! " + "This is \(name)!"
print(greeting) // > "Hi! This is Swift!\n"
var greeting = "Hi! " + "This is \(name)!"
print(greeting) // > "Hi! This is Swift!\n"
Getting a subrange of a string is always a better idea
as it becomes a bit more readable. With the Objective-C the programmer have to
use the substringWithRange() and NSRange. That will look
something like the code that has been depicted below:
[name substringWithRange:NSMakeRange(1,2)];
Now let’s see the process of getting the
subrange of a string with the Swift in
Objective C:
let start = str.index(str.startIndex, offsetBy:
1)
let end = str.index(str.startIndex, offsetBy: 3)
let range = start..<end
print(string.substring(with: range))
let end = str.index(str.startIndex, offsetBy: 3)
let range = start..<end
print(string.substring(with: range))
Please note that the Swift has a support of the
tuples.
·
Comparison
Since when there is a transformation from the objective C to swift, the comparison
part has become the easiest one. Look at the example that has been depicted
below for more details:
let name1 = "Bob"
let name2 = "Tom"
print(name1 < name2) // > True
let name2 = "Tom"
print(name1 < name2) // > True
·
For loops
In this transformation, there is a great change
happened that is allowing the use of the for loops. Now
the programmers can use a range of numbers with for loops. Here’s a code that
is depicting the same:
for i in 0..<10 {
print(i)
}
Apart
from this the programmers can now add condition to for loops
with where statement that is basically opposed to using if statement.
Here’s an example that will be depicting
the same:
for
i in 0..<10 where i % 2 == 0{
print(i)
}
print(i)
}
·
Switch statements
Swift
switch statements generally permits the programmer to use not only the numbers
but strings also. Here is an example of the same that has been depicted below:
let
name = "Bob"switch name {
case "Bob":
print("Bob")
case "Tom":
print("Tom")
default:
print("Does not match.")
}
Programmers
can also use the range of numbers and conditions can be also be specified with
the where statements. Here’s an example that is resolving that for your
understanding:
let
number = 9
switch
number {
case
0...5:
print("0 to 5")
case
let x where x == 6:
print("It's 6")
default:
print("Not defined")
}
· Functions
Unlike
most of the functions in the Objective-C the function parameters are generally constants
by default in the Swift. They are generally known to be equivalent of constant
that has been declared with let. In case, the programmer wants to modify
the parameters in functions then they can use the inout statement.
Read More......
Also Visit Here - PHP Projects Free Download with Source Code
Comments
Post a Comment