The Complete Guide For: By: Hossam Ghareeb
The Complete Guide For: By: Hossam Ghareeb
Programming Language
Check examples:
Variables & Constants.
Variables & Constants.
In Objective-C we used to use mutability, for example:
NSArray and NSMutableArray or NSString and NSMutableString
In Swift, when you use var, all objects will be mutable BUT when you
use let, all objects will be immutable:
Printing Output
● We introduced the new way to print output using println(). Its
very similar to NSLog() but NSLog is slower, adds timestamp to
output message and appear in device log. Println() appear in
debugger log only.
● In Swift you can insert values of variables inside String using "\()" a
backslash with parentheses, check example:
Type Conversion
Swift is unlike other languages, it will not implicitly convert types of
result of statements. Lets check example in Obj-C :
In Swift you can't do this. You have to decide the type of result by
explicitly converting it to Double or Integer. Check next example:
Type Conversion
Here we should convert any one of them so the two variables be in same
type.
Swift guarantees safety in your code and makes you decide the type of
your result.
If
● In Swift, you don't have to add parentheses around the condition.
But you should use them in complex conditions.
● Curly braces { } are required around block of code after If or else.
This also provide safety to your code.
If
● Conditions must be Boolean, true or false. Thus, the next code
will not work as it was working in Objective-C :
● Use let with If to check the Optional value. If the optional value is
nil, the conditional will be false. OtherWise, the it will be true and
the value will be assigned to the constant of let
Check example:
If With Optionals
Switch
Switch works in Swift like many other languages but with some new
features and small differences:
● It supports any kind of data, not only Integers. It checks for
equality.
● Switch statement must be exhaustive. It means that you have to
cover (add cases for) all possible values for your variable. If you
can't provide case statement for each value, add a default
statement to catch other values.
● When a case is matched in switch, the program exits from the
switch case and doesn't continue checking next cases. Thus, you
don't have to explicitly break out the switch at the end of each
case.
Check examples:
Switch
Switch Cont.
● As we said, there is no fallthrough in switch statements and
therefore break is not required. So code like this will not work in
Swift:
You can decompose the values of tuples with many ways as you will
see in examples. Most of time, tuples are used to return multiple
values from function. Also can be use to enumerate dictionary
contents as (key, value). Check examples:
Switch With Tuples.
● Decomposing:
● With dictionary:
Switch With Tuples.
Using tuples with functions:
Switch With Tuples.
Now we will see tuples with switch. We will use it in checking that a
point is located inside a box in grid. Also we want to check if the point
located on x-axis or y-axis. Here is the gird:
Switch With Tuples.
Switch With Value Binding
You can bind the values of variables in switch case statements to
temporary constants to be used inside the case body:
Switch With "Where"
"Where" is used with case statement to add additional condition.
Check these examples:
Switch With "Where"
Another example in using "Where":
Loops
● Like other languages, you can use for and for-in loops without
changes. But in Swift you don't have to write the parentheses.
● for-in loops can iterate any collection of data. Also It can be used
with ranges
Functions
● Functions are created using the keyword 'func'.
● Parentheses are required for functions that don't take params.
● In parameters you type the name and type of variable between ':'
● In Swift, you can use default parameter values. BUT be aware that
when you wanna use function with default-valued params, you
must write the name of the argument when you wanna use it.
Check examples:
Functions
● Using default parameter value:
● Check examples :)
Dictionaries
Enum
● Enum is very popular concept if you have specific values of
something.
● Enum is created by the keyword 'enum' and listing all possible
cases after the keyword 'case'
Enum
● Enum can be used easily in switch case but as we know that switch
in Swift is exhaustive, you have to list all possible cases.
Enum With Associated Values
● Enum values can be used with associated values. Lets explain with
an example. Suppose you describe products in your project, each
product has a barcode. Barcodes have 2 types (UPC, QRCode)
● You can initialize an enum value using its constant value using this
format EnumName(rawValue: value). It returns the enum that
map to the given value. Be careful because the value returned is
Optional, it may contain an enum or nil, BECAUSE Swift can
guarantee that the given constant is exist in enum or not.
Enum With Raw Values Example:
Enum With Raw Values Example:
● Raw values can be Strings, Chars, Integers or floating point
numbers. In using Integers as a type for raw values, if you set a
value of any case, others auto_increment if you didn't specify
values for them.
Thanks
We have finished Part 1.
In next parts we will talk about Classes, Structures, OOP and some
advanced features of Swift.
If you liked the tutorial, please share and tweet with your friends.