Cover image for My first week of learning Swift.

My first week of learning Swift.

s_awdesh profile image Awdesh Updated on ・3 min read

1.) Declaring constants and variables.



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


2.) Declaring a function


Java's syntax of a regular function looks like below


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

Python's syntax of a class function

python class

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-:


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


5.) Type Alias

As the name suggests, type alias allows giving aliases to existing types.

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.


6.) Closures

As per Apple docs

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


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.


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


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


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.


8.) Dictionary and Array


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


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

Posted on by:

s_awdesh profile



Learner, philomath. Engineer by choice humorous by nature.


Editor guide

in 7) What you really should do to print the "name", is unwrapping the optional with Optional Binding (if-let) Example:

if let name = name {

With this approach, you don't need to verify first if it is nil and then force unwrap it. (Normally force unwrapping things in swift is a bad practice, unless is Outlets or if you don't have any other option for some kind of reason)


Perfect. That's a great input.

I have updated the article now.


Great post, it would have been a nice follow up to discuss optional bindings and chaining after your section on optionals!


Thanks for the feedback Jocab.

I'll try to read more about the optional chaining and binding and write about it in near future. Although I found Stack Overflow article ( referenced ) very informative.


Please post article on handling keyboards with textfields, validating textfields for something and throw error.

Sloan, the sloth mascot Comment marked as low quality/non-constructive by the community View code of conduct

Hello, I'm IOS developer. Recently, when developing applications using swift, I found that there are few caches written in pure swift. So I wrote a cache - swift cache, which is a lightweight general IOS cache library using swift 5. If you are using swift for development and need to use cache, maybe you can try swiftlycache, maybe you will like it, if you can also introduce it to your friends. Thank you