Merhaba arkadaşlar bu yazımda benimde yeni ögrenmeye başladığım bir dil olan swift ile alakalı buldugum bir eğitim dokumanını paylaşmak istiyorum. Bana çok faydası olsu inşallah size de olur iyi çalışmalar dilerim

Firstly, you will have to download and install Xcode 6. Once you have installed it, open it up and select File from the menu -> New -> Select Source on the left under either iOS or OSX -> Playground. Give your playground a name and you are ready to get started.
Alternatively, you could use the REPL (Read Evaluate Print Loop) from the terminal.
Instructions to run from the terminal:
1. Open up terminal
2. If you have two or more versions of Xcode installed then you will need to select Xcode 6 as your default version. If you are only running Xcode 6 then skip ahead to step 3. If you are only running Xcode 6 then skip ahead to step 3, otherwise go ahead and run the following line:
sudo xcode-select -s /Applications/Xcode6-Beta.app/Contents/Developer/
At the time of writing this post beta version of Xcode 6 was named “Xcode6-Beta”. Please check your app name in the “Applications” folder to write out the appropriate path when using xcode-select.
3. To start the REPL type:
xcrun swift

Fundamentals

VARIABLES

As with every programming language you have variables which allow you to store data. To declare a variable you have to use the keyword var.

The above code instructs the system that you want to create a variable named greeting which is of type String and it will contain the text, “Hello World”.
Swift is smart enough to infer that if you are assigning a string to a variable and in fact that variable will be of type string. So you need not explicitly specify the type as in the above example. A better and common way of writing the above example would be:

Variables can be modified once created so we could add another line and change our greeting to something else.

While writing an application there are many instances where you don’t want to change a variable once it has been initialized. Apple has always had two variants of types mutable and immutable. Mutable meaning the variable can be modified and immutable that it cannot be modified. They prefer immutability by default which means that the values aren’t going to change and it makes your app faster and safer in a multi-threaded environment. To create an immutable variable you need to use the keyword let.
If we change our greeting example to use let instead of var then the second line will give us a compiler error because we cannot modify greeting.

Lets take another example so you understand why and when to use let.

The above example not only shows us the various types that are available in Swift but it also shows us that the reason to use let. Aside from the version number of the Swift language everything else remains constants. You might argue that isAwesome is debatable but I’ll let you reach that conclusion once you reach the end of this post.
Since the type is inferred we should simply write:

STRINGS

In our above example we have been writing the String type. Lets see how we can concatenate two strings by using the + operator.

Strings have a powerful string interpolation feature where it’s easy to use variables to create a strings.

In all the above examples, I have been using the keyword let which means you cannot modify the string once it has been created. However, if you do need to modify the string then simply use the keyword var.

OTHER TYPES

Besides strings you have Int for whole numbers. Double and Float for floating-point numbers and Bool for boolean values such as: true of false. These types are inferred just as a string so you need not explicitly specify them when creating a variable.
Float and Double vary in precision and how large of a number you can store.
  • Float: represents a 32-bit floating-point number and the precision of Float can be as little as 6 decimal digits.
  • Double: represents a 64-bit floating point number and has a precision of at least 15 decimal digits.
By default when you write a floating-point number it is inferred as a Double.

You can explicitly specify a Float.

Collection Types

ARRAY

Collections come in two varieties. Firstly, an array which is a collection of data items which can be accessed via an index beginning with 0.

You can create two types of arrays: an array of a single type or an array with multiple types. Swift is keen on being safe so it prefers the former but can accommodate the later with generic types. The example above is an array of strings which means that it is a single type array.
To access an item from the array you need to use the subscript:

Note: we used a function above called println which will print the value “Jack” to the console and then add a new line.

MODIFYING AN ARRAY

Lets create a new array that contains a todo list.

Make sure that you use the keyword var so that we can modify our array.
To add another item to our todo array we use the ‘+=’ operator:

To add multiple items to our todo array we simply append an array:

To replace an existing item in the array simply subscript that item and provide a new value:

To replace an entire range of items:

DICTIONARY

The other collection type is a Dictionary which is similar to a Hash Table in other programming languages. A dictionary allows you to store key-value pairs and access the value by providing the key.
For example, we can specify our cards by providing their keys and subsequent values.

Above we have specified the card names as the keys and their corresponding numerical value. Keys are not restricted to the String type, they can be of any type and so can the values.

MODIFYING A DICTIONARY

What if we wanted to add an “ace” to our cards dictionary? All we have to do is use the key as a subscript and assign it a value. Note: cards is defined as a var which means it can be modified.

We made a mistake and want to change the value of “ace”. Once again just use the key as the subscript and assign it a new value.

To retrieve a value from the dictionary

Control Flow

LOOPING

What it good is a collection if you cannot loop over it? Swift provides whiledo-while,for and for-in loops. Lets take a look at each one of them.
The easiest one of them is the while loop which states while something is true execute a block of code. It stops execution when that condition turns to false.

Note: the exclamation mark before the variable complete denotes not and is read as “not complete”.
Likewise, you have the do-while loop which ensures that your block of code is executed at least once.

Subsequent calls to the println statement will print “Downloading..”
You have the regular for loop where you can specify a number and increment that number to a certain value:

Or you can simply use the for-in variant where it creates a temporary variable and assigns it a value while iterating over the array.

The above code will print out all the card names in the array. We can also use a range. A range of values is denoted by two dots or three dots.
For example:
  • 1…10 – is a range of numbers from 1 to 10. The three dots are known as a closed range because the upper limit is inclusive.
  • 1..<10 – is a range of numbers from 1 to 9. The two dots with a lesser-than sign is known as a half-closed range because the upper limit is non-inclusive.
Lets print out the 2 times table using for-in with a range:

We can also iterate over the cards dictionary to print out both the key and the value:

IF STATEMENTS

To control the flow of our code we of course have an if statement.

Note: The if syntax can have parenthesis but they are optional. However, the braces {} are mandatory unlike other languages.

SWITCH STATEMENT

The switch statement in Swift is very versatile and has a lot of features. Some basic rules about the switch statement:
  • It doesn’t require a break statement after each case statement
  • The switch is not limited to integer values. You can match against any values such as: StringIntDouble or any object for that matter.
  • The switch statement must match against every value possible if not you must have a defaultcase which makes your code safer. If you don’t provide a case for every value or a defaultthen you will get a compiler error saying: “switch must be exhaustive”.

Lets say you have a distance variable and you are trying to print a message based on distance. You can use multiple values for each case statement:

There are times when even multiple values are limiting. For those instances you can use ranges. What if any distance greater than 10 and less than 100 was considered far?

Can you guess what the above code will print?

Functions

Finally, we have been using println in a lot of our examples which is an example of how to use a function. But how can you create your own function?
It is simple, you have to use the keyword func and provide it with with a name.

What good is a function if it always going to just print “Queen” as the card name. What if we want to pass it a parameter so it can print any card name?

Of course, we are not restricted to just one parameter. We can pass in multiple parameters:

What if we simply wanted our function to build a string and return the value instead of printing it? Then we can specify a return it which is specified at the end of the function declaration followed by an array ->.

Above we are saying that we are creating a function named buildCard which takes in two parameters and returns a String.
Ramazan CESUR…