In introduction I only say one thing about Swift. Swift is a new language launched by Apple. Everybody know about swift in these days because it is a very big breakthrough by Apple to launch new language that’ll replace Objective C in future. So everybody know why they invent new language. Yup but some key points I am writing below.
- Fast Development
- Easy to Learn for new born babies 🙂
- Modern ( Take best features from many other new languages and combine them )
- Read-Eval-Print-Loop (REPL)
- Safe (In perspective of bugs or surprises)
- Flexible ( You can use as Functional Programming or OOP )
To start development of Swift we need Xcode 6 and if you have Xcode 6 its mean you have the required toolset to begin 🙂 .
In this course I will consider, you know any OOP Language (Java, C++ etc.). If you are not familiar with OOP concept in programming then I think you will face little difficulty here. As I won’t be discussing basic programming and OOP concepts here. So if you know about these terms then you take this course and if you did not know, this course is still strongly recommended for you. Only use a small tip.
Tip: Like you did not know what is a data type now open Google and write what is data type. And when you feel comfortable start next topic. 🙂 . In this way you can learn Swift and in bonus you learn basics of any programming language.
“A read–eval–print loop (REPL), also known as an interactive top level or language shell, is a simple, interactive computer programming environment that takes single user inputs (i.e. single expressions), evaluates them, and returns the result to the user; a program written in a REPL environment is executed piecewise.” (Wikipedia)
Sorry for above boring part 🙁 but required here.
Now start my favorite part. Lets do some PRACTICAL WORK WOW
We use REPL in Xcode by using Playground which we will discuss in next section. In this section we are using Terminal for REPL.
Now in these days, mostly we have more than one Xcode installed on machine like one stable version and one beta version. So before start working, open Terminal and execute command.
If you see the response like above image, its mean you can start work. Otherwise change your selection using this command.
sudo xcode-select -s /Applications/Xcode-Beta.app/Contents/Developer
You can replace Xcode name in the above path with your Xcode app name. Mine is Xcode-Beta.app so I am using it. You can find Xcode under Applications directory. After changing the path, confirm again using xcode-select -p command.
Now use command xcrun swift
If you see content like above image its mean you are ready to start programming. Write println(“Hello Universe”) and press enter and you got response as below image.
Now I am writing a small expressions. Only copy paste or write on your own on your terminal and see results.
- let text = “We are great”
- let answer = 4*5-9*1.1/2
- var coins = 10
- let result = 3 * coins
After every expression you get some result, only see that and do not take tension if you don’t understand what these expression mean. Because I am here 🙂 to explain you all these in the upcoming lessons.
Now one more example but this time create a new file using any Editor with name ChallengeMe.swift and write these lines in that file.
let name = “Hafiz Waleed Hussain”
println(” Your Name is \(name)”)
And run command from Terminal written below.
xcrun swift ‘file path’
And your result will look like
Now I think you can practice more if you want, but from my side i am starting next topic.
“A playground is a place with a specific design to allow children to play there. It may be indoors but is typically outdoors. While a playground is usually designed for children, some playgrounds are designed for other age groups. Berlin’s Preußenpark for example is designed for senior adults age 70 or higher. It is possible for a playground to exclude children if they are below the required age for entrance.” (Wikipedia)
I think that definition is great to define Playground. 🙂
According to my perspective if you change the above children word with Programmer. You got the good definition of Xcode playground.
“A playground is a place with a specific design to allow Programmer to play there.”
Okay lets start by Official playground definition.
“Playgrounds enable the interactive experience of a script language, even though Swift compiles to highly-optimized native code. Type a line of code and the result appears immediately. If your code runs through a loop, add that line of code to the timeline assistant editor to watch its progression. Display variables in a graph, inspect each step when drawing a view, or watch an animated SpriteKit scene. When you’ve perfected your code in the playground, simply move it into your project. Playgrounds documentation includes tutorials that you can open in a playground, providing an interactive worksheet for experimentation. ”
Boring… I know but required here.
In Simple words like we use Terminal. And in Terminal we face some difficulty so Xcode give us an interactive worksheet where we write our code and get result immediately like in REPL on Terminal. Now question is, What is the benefit of Playground? so for that I am start working in Playground and you’ll easily know what are the benefits of Playground.
Open Xcode and follow below images.
After that, press Next and save where you want then press Create Button and you will see the Playground like in below image.
Now in next image first I show you all parts of the Playground because in above image some are disable or hidden by default.
Now lets go through them one by one.
Simple in which we write swift code.
Result Sidebar: (Official name i did not know because official swift guide use side bar for that)
When you write a code in code editor and go to next line or wait for a while you easily see the result on Result Sidebar. Important thing this is not a Console, it only show you result immediately like we press enter in REPL Terminal and got the result but here immediately code compile. Like in above image “Hello, Playground” Against the code line ‘ var str = “Hello, playground” ‘. So that is not a Console result.
As you see there are two buttons when you hover a line First is Quick Look and second one Value History.
If you press the quick look button. You got a popup with some output. For example if you click on the string code line, you got a string internal value on that popup like “Hello …”. And if you write some code for Graphics like you create an image then you press the Quick Look button you got the popup with image which are assign to that graphics variable.
If you feel any ambiguity then see below images with focus and clear your ambiguity.
If you already open the Assistant Editor then you press Value history. You got the history of the value which is available in the variable. And if you did not open Assistant Editor it automatically open after pressing Value history button. Or if you want to open manually then follow the instruction Press on Toolbar View -> Assistant Editor -> Show Assistant Editor.
And you will see the Assistant Editor like in below images.
Right image is empty and left image having some values for different operations.
Now If you have any task which repeat like a loop you will be get Timeline graph on Assistant Editor as shown in above left image. If you have some string then you got a result like second rectangle in left above image. If you have println( Some value ) than you will got a result on Console output window which third rectangle in above left image.
I think it is enough for the intro. So Guys now start to Play with Playground and I remind you again our definition of Playground.
“A playground is a place with a specific design to allow Programmer to play there.”
7 thoughts on “Swift Programming Language: Introduction”
Typically I do not understand post in blogs and forums, however i want to state that that write-up incredibly pushed me to have a look during along with do it! Ones writing preference is shocked everyone. Thanks, pleasant publish.. digital finance
You are so interesting! I don’t suppose I’ve read anything like that before.
So wonderful to discover someone with a few original thoughts on this issue.
Seriously.. many thanks for starting this up. This website
is something that is required on the web, someone with a little originality!
Great article. I’m dealing with a few of these issues as well..
Hi, always i used to check webpage posts here early in the dawn, for the reason that
i enjoy to gain knowledge of more and more.
I get pleasure from, result in I found exactly what I used
to be looking for. You’ve ended my four day long hunt!
God Bless you man. Have a nice day. Bye
fantastic publish, very informative. I ponder why the
other specialists of this sector do not understand this.
You must continue your writing. I’m confident, you’ve a great readers’ base already!
I loved your blog post.Really thank you! Really Great.