THIS CONTENT DOWNLOAD SHORTLY

Objective

The main objective of this post is to describe that how to play with swift programming code in iOS. If you play with all code part with new swift language than you play here with on the spot output.

 

Introduction

In this post, I’ll show you how you can easily write swift code and each & every line display output on the right side of the screen. We will simply create a playground area for swift programming language implementation and check different type of things on the screen.

First of all, you must have OS-X 10.9.4 version or later and Xcode-6 Beta version for swift playground. Following are the steps that give you a brief introduction about how to play swift code in playground.

 

Step 1 Create Playground Area

Open up Xcode and create a playground with clicking on “Get started with a playground” which looks like: 

welcome to xcode

After that, give your playground name and choose platform iOS or OS X does not matter because we don’t interact with user interface and click next and save to your location Which looks like: 

xcode choose file

 

Step 2 Main Screen of Playground

Finally open screen is like: 

playground 1

Here, left hand side white part is used as editor, here, you can write Swift program and right hand side gray part is used as view Result of your code. Here, one example is by default create for var str = “Hello, playground” and similarly, display result on right side.

 

Step 3 Play With if-else Statement

Now, put some swift code for testing like if-statement, for-loop statement etc.: 

playground-2

Here, you write something and show immediate result on the right hand side. For for-in statement they display how many times execute the loop. It’s easy to identify our loop.

 

Step 4 Play with array type

Using for-loop statement or for-in statement. We can make one array arrayNumber with below statement:

var arrayNumber = [1, 2, 3, 4, 5, 6]

Now, for accessing these array elements we can write for loop in swift like:

for var i=0; i<arrayNumber.count; i++ {
println(i)
}

OR

for value in arrayNumber {
println(value)
}

For, beginner levels sometimes hard to identify how many times loops are executed. So, playground provide facility with display no. of times loop execute in right side result window. Which shows in below figure: 

playground-3

 

Step 5 Play with Loops

While using dynamic array for-in statement. If array size is not fixed or using for-in loop the swift provide “. . .” syntax in for-in statement. For that you can write:

for value in 0…10 {
println(value)	// 11 times
}

This loop print 0 to 10 so, it executes 11 times. But, if arrayNumber is array that has size 6, so those 6-element prints if loop execute only 0 to 5 (arrayNumber.count - 1) times.

So, for that in swift, we can also, access these elements by:

var arrayNumber = [1, 2, 3, 4, 5, 6]
for valueOfArray in 0..

Here, for example

0…10 means starting from 0 and ending with 10 and print whole numbers are in between 0 to 10. 0 … < 10 means starting from 0 and ending with 9 and print whole number in between 0 to 9.

So, here we can use 0…

These loops in playground looks like: 

playground-4

 

Step 6 Graph in Playground

Now, chart in playground for your observation: Write one loop which perform some arithmetic operation like:

var sum: Int = 0
for value in 0…10 {
sum = sum + value	// 11 times
}

After writing code move mouse focus on (11 times) in playground (right hand side). You show two icon one in for quick look and another is “+” sign. Click on”+” sign for chart. Which looks like: 

playground-5

On clicking that button you can show chart on right hand side which looks like: 

playground-6

This chart made by arithmetic equation, which runs in for-loop. You can easily show value by scrolling below red vertical bar, which is located under the graph. This for-loop execute (11 times) so, 11 point shown on the graph and each point has some value. Show in below figure: 

playground-7

For final result of sum is just write sum variable in the left hand side code editor part and right hand side it’s show result like: 

playground-8

Conclusion:

After doing all these things, we conclude that for beginner of swift programmer play board is most useful to know how each & every code works. In playground project, we can implement small part of code, which we want to check, and using this we are not handling whole project or entire structure only work with one file, which is myPlayground.playground file.

Got an Idea of iPhone App Development? What are you still waiting for? Contact us now and see the Idea live soon. Our company has been named as one of the best iPhone App Development Company in India.

I am iOS Application Developer & I personally believed that When a programming language is created that allows programmers to program in simple English, it will be discovered that programmers cannot speak English.