This is my first attempt EVER to post a tutorial blog so just bear with me :P
Criticism(constructive/destructive/troll) is most welcome.
You probably know what Swift is since you are here but still here's a quick intro...
Swift is a programming language newly announced by Apple at the WWDC 2014. Swift will/can be used for developing iOS as well as Mac OS X applications, which were previously developed in Objective C/ Objective C++.
Also announced was iOS 8. Apps developed in Swift would work with iOS 7 and 8. Swift can be used in conjunction with Objective-C. I'll probably talk about that in future articles...probably being the key word there.
- To code in Swift for iOS/ Mac OS X, you need to download Xcode 6 Beta from Apple Developer.
- Here's the link: https://developer.apple.com/xcode/downloads/
- If you are only interested in learning the language without any of the app sandbox stuff, follow these steps
- After installing Xcode 6, Go to File->New->Project.
- Select Application under OS X front he left panel.
- Choose "Command Line Tool" -> Next.
- Enter the project name, Choose Language as "Swift" obviously. -> Next.
- Select Location. -> Finish.
- Profit.
- After installing Xcode 6, Go to File->New->Project.
- Select Application under OS X front he left panel.
- Choose "Command Line Tool" -> Next.
- Enter the project name, Choose Language as "Swift" obviously. -> Next.
- Select Location. -> Finish.
- Profit.
You probably know about Objective C, its syntax blah-blah...If you don't, you're probably better off learning that first. Also ...
BOOOOOOOOAAARING!
Enough with the technical blah-blah...Lets get to the action.
I'm assuming you are familiar with Cocoa Touch frameworks or at least some of them, and you have an idea about Objective-C syntax, or at the very least some idea about programming and its concepts C'mon. Even if you don't, and you are an aspiring iOS developer, you need to know what Swift is all about. So read on...it won't hurt...probably.
Getting Started: THE VARIABLE
The root of all temporary/permanent storage of values inside the code..
ALL HAIL THE MIGHTY "variable".
Declarations:
var myNumber = 1
Objective-C coders must be thinking "BOOOO! Wheres the semicolon? Wheres the data-type?
Well, there's no need of those anymore.
Although in some cases you do have to mention them. Here's how...
var name : String
The above code declares a variable named "name" of type String (Objective-C uses NSString).
However, the following code generates an error...
var myVariable
The compiler thinks...WTF? How do I know what type it is? ERROR!
So either you gotta assign it a value like...
var myVariable = 1
OR
var myVariable = 2.0
OR
var myVariable = "WOW THIS IS EASY"
All the three declarations are correct. They tell the compiler if "myVariable" is an int, double or String respectively. (Objective-C coders may notice the lack of @ before quotes.
Alternatively, specify its data-type if you don't wanna initialise it like so...
var myVariable : Int
So, in short its like...
var <userDefinedName> : <data-type>
Constants
Constant variables are declared using a keyword called "let"as...let myConstant : Int = 10
Same works for strings, double etc.
<Quick-Tip> Data types now begin with a Caps letter
Arrays
Arrays are declared as...
var platforms = ["PS3", "X360"]
OR
var platforms : Array<String> = ["PS3", "X360"]
This creates an array of two strings. Again, specifying data-type is upto you, but look closely. The second statement has a "< String >" along with the "Array" type. Works for other types too.
This tells the compiler that the array will only contain String data-type. Java coders should already know this.
This tells the compiler that the array will only contain String data-type. Java coders should already know this.
Dictionaries
For those who dunno, a dictionary is a data-type which associates a key to a value. A key and its value are separated by a colon. A dictionary can have any number of key value pairs.
Kinda like this...
Key : Value
The data type of the value may vary. For more info on this... Google it!
Kinda like this...
Key : Value
The data type of the value may vary. For more info on this... Google it!
var gamesDict = [ "name" : "GTA V", "rating" : 5, "platforms" : ["PS3", "X360"], ]
This creates a dictionary variable named "gamesDict" and maps a String, Int and an Array to the respective keys.
All Key-Value pairs are separated by commas, even the last pair.
Accessing a value for a particular key
var gameName = gamesDict["name"] var gameRating = gamesDict["rating"]
This assigns gameName as "GTA V" and gameRating as 5.
While accessing the values as above is fine, the compiler will give you a warning as its unpredictable(to the compiler) what will be the data-type of value for the mentioned key.
So a better way would be...
var gameName = gamesDict["name"] as String var gameRating = gamesDict["rating"] as Int
^this is sort of a type cast. Be careful with this though. This can cause crashes/errors later if you don't set the type right. When in doubt cast it as "AnyObject". More on that in future parts. Google it if you are impatient!
The above dictionary had values with different data-types. But if you wanna make a dictionary which you know will only have values of a single data-type, or you don't wanna allow any other type to be added, use this...
var gamesDict : Dictionary<String, String> = [ "name" : "GTA V", "rating" : "M" ]
Printing on the Console
Use println() function to print Strings on the debug console. Java guys would be familiar.
println("GIMME A HELL YEAH")
var nOne = 2 var nTwo = 5 var sum = nOne + nTwo println("Addition is \(sum)")
This would print "Addition is 7"
Conditions: if...else block
var nOne = 2 if nOne > 0 { println("less than zero") } else { println("greater than zero") }
The FOR statement
var games = ["GTA V", "WATCH DOGS", "SUPER MARIO"]
There are many ways of looping through the above array...
for temp in games { println("GAME NAME: \(temp)") }
for i in 0..3 { println("GAME NAME: \(games[i])") }
3 is not included.
Array elements are accessed by array_name[i] where i is an integer index.
for( var i = 0; i < 3; ++i) { println("GAME NAME: \(games[i])") }
All of the above print the three strings in the games array one after another(on a new line obviously).
The WHILE statement
var n = 100 while n < 0 { n = n + 10 }
The SWITCH statement
var platforms = ["PS3", "X360"] for platformVar in platforms { switch platformVar { case "PS3": println("Playstation Supported!") case "X360": println("Xbox 360 Supported!") default: println("No Console Version!") } }
^Again, no parenthesis needed, Iterate through the array via temp variable, check its value via switch statement, and print accordingly.
And Yes, Switch works on String variables too. ^_^
Well thats it for this part. Soak it in.
In the next article, Its Functions, Classes, SubClasses and Objects.
In the next article, Its Functions, Classes, SubClasses and Objects.
Feel free to copy and mess around with the snippets in Xcode. Lemme know if there are errors.
Peace!


Thanks for sharing good information
ReplyDeleteiOS App Development Online Course