My first week of learning Swift.
Tags: swift, programming, beginners, ios
Published: 2018-09-14 05:15:25

---
title: My first week of learning Swift.
published: true
description: Few key concepts of Swift programming language.
tags: swift, programming, beginner, ios
cover_image: https://thepracticaldev.s3.amazonaws.com/i/ibnphch28sc3uz9f1opl.jpeg
---

### 1.) Declaring constants and variables.

`Variables`

![var](https://thepracticaldev.s3.amazonaws.com/i/j30gzph8h08vnshfg6ib.png)

`Constants`: Once declared their value can't be changed. Swift will throw compile time error when trying to update the value.

![let](https://thepracticaldev.s3.amazonaws.com/i/0czj40r38b68uvy85pwa.png)

### 2.) Declaring a function

![func](https://thepracticaldev.s3.amazonaws.com/i/a7xk2qy0dg795fmbwmkp.png)

Java's syntax of a regular function looks like below

![method](https://thepracticaldev.s3.amazonaws.com/i/z1lkguxfj9wxwvxrjy8n.png)

### 3.) Class function

Like python, swift has class functions. Class functions are bound to a class rather than an instance of a class. Honestly, `function` followed by `class` in the syntax threw me off a bit when I first encountered it in the source code.

![class functions](https://thepracticaldev.s3.amazonaws.com/i/1oafqmyxd1wy5ik98wpp.png)

Python's syntax of a class function

![python class](https://thepracticaldev.s3.amazonaws.com/i/bitdtez1x7g1k6ckwgrd.png)


### 4.) Using underscore `_`.

In my very short experience with Swift, I have seen the use of `_` inside function calls. `_` is used to define an unnamed parameter inside the function. Swift uses a name for each parameter so when we call a function we need to provide the name of the parameter. 

Use of `_` can bypass passing of the name altogether. Let's walk through an example-:

![underscore](https://thepracticaldev.s3.amazonaws.com/i/e8huvrtk19shv64kr2ut.png)

In general, when `_` in front we can to ignore value. Another use case of `_` would be something like below.

![return](https://thepracticaldev.s3.amazonaws.com/i/zv8817iurxim6bchrzc1.png)

### 5.) Type Alias

As the name suggests, type alias allows giving aliases to existing types. 
![alias](https://thepracticaldev.s3.amazonaws.com/i/ev86b2bi4o2fiocq7lbg.png)

This is certainly not the best use case of using type aliases. The better use case is by aliasing closures inside the function. Functions and closures in swift have a parameter type and a return type which can be aliased.

![alias](https://thepracticaldev.s3.amazonaws.com/i/8x93lvorzyajh6pjmhg6.png)

### 6.) Closures

As per Apple docs

> Closures are self-contained blocks of functionality that can be passed around and used in your code.

![closures](https://thepracticaldev.s3.amazonaws.com/i/cnn5n4jrh34up45e1kcx.png)

### 7.) `?` and `!`.

Both `?` and `!` is used for Optionals in swift. Use of optionals can make variable to have a value or nil.
 
`?` is used after the variable type indicate that the variable is optional.

![Optional](https://thepracticaldev.s3.amazonaws.com/i/4p134d6dmh4go5oh31v7.png)

`!` is used to unwrap the value of optional so to use it.

![unwrap](https://thepracticaldev.s3.amazonaws.com/i/io9a2pikn4vs0xorvqmu.png)

We could check for non-nil before unwrapping optional to get rid of runtime error.

![force-unwrap](https://thepracticaldev.s3.amazonaws.com/i/0lortmj1krhaeqz5riia.png)

As pointed out by Nuno Vieira in the comment section, force unwrapping is not a good practice rather we can use (if..let) which is called **Optional binding** method.

![optional-binding](https://thepracticaldev.s3.amazonaws.com/i/xg7v2d9w42otdxjwkjaq.png)

### 8.) Dictionary and Array
![array](https://thepracticaldev.s3.amazonaws.com/i/cd6wxoa03b3r5ty6qza3.png)

This is it for this post. Stay tuned for articles like this.

### References
* Offical swift tutorial from Apple-: https://docs.swift.org/swift-book/GuidedTour/GuidedTour.html
* Great read about Optionals-: https://stackoverflow.com/questions/24003642/what-is-an-optional-value-in-swift

---

If you want me to write on a specific topic, please feel free to post them in the comment section. 

You can support my work by buying me a coffee below. 💚💚💚💚💚💚 !!
[![Buy me a ko-fi](https://thepracticaldev.s3.amazonaws.com/i/5xarm26ij4p1hwuqh9vr.png)](https://ko-fi.com/F1F0J8U1)