Apple recently announced a pretty major change to the way iOS apps have been developed in the past, an entirely different programming language called Swift which replaces Objective-C.
In my efforts to learn the new language, I’ve decided I will be posting regularly as I step through the learning process, sharing everything I find. This is the first post of many on the topic, and I hope you decide to follow along!
In my efforts to learn the new language, I’ve decided I will be posting regularly as I step through the learning process, sharing everything I find. This is the first post of many on the topic, and I hope you decide to follow along!
The Basics
Declaring Variables:
iOS8 Swift does away with the standard of declaring variables by starting with their type names, and instead opts to use a Javascript-like ‘var’ keyword to define any variable.
Objective-c: NSString *firstString=@"My first string";
Swift: var firstString="My first string"
Declaring Constants:
Objective-c: NSInteger *const myfirstconstant;
Swift: let ksomeconstant:int=10;
Meanwhile constants are expressed with the ‘let’ keyword.
Declaring arrays and dictionary:
Objective-c: NSArray *Myarray=[[NSArray alloc]init];
NSDictionary *Mydictionary=[[NSDictionary alloc]init];
Swift: var Myarray=["one","two","three"]
var Mydictionary=["primary number:one","secondary number:two"]
There’s a lot more to go over, but I think these basics are important to get a start going on to the tutorial. So with that, let’s move on to Hello World.
HELLO WORLD Program in Objective-c and Swift:
Objective-c:
#import
#import<Foundation/Foundation.h>
int main(void)
{
NSLog(@"Hello,World!\n");
return 0;
}
Swift:
1.Create a new x-code project in single view application.
2.Select SWIFT language.
3.Find AppDeligate.swift file inside the project
Inside of this file find the line that says:
“// Override point for customization after application launch.”
4.Replace this line with our amazing hello world code:
println("Hello World")
Now press run and you should see a blank app boot up, and the words “Hello World” print to the console. Congratulations! You just wrote your first iOS8 Hello World application in Swift!

No comments:
Post a Comment