DEV Community

Cover image for My first week of learning Swift.
Awdesh
Awdesh

Posted on • Edited on

My first week of learning Swift.

1.) Declaring constants and variables.

Variables

var

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

let

2.) Declaring a function

func

Java's syntax of a regular function looks like below

method

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

underscore

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

return

5.) Type Alias

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

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

6.) Closures

As per Apple docs

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

closures

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

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

unwrap

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

force-unwrap

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

8.) Dictionary and Array

array

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

References


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

Top comments (7)

Collapse
 
nunovieira profile image
Nuno Vieira • Edited

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 {
       print(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)

Collapse
 
s_awdesh profile image
Awdesh

Perfect. That's a great input.

I have updated the article now.

Collapse
 
jacobboyd profile image
Jacob Boyd

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

Collapse
 
s_awdesh profile image
Awdesh

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.

Collapse
 
tofulosophy profile image
tofulosophy

Nice article!

Collapse
 
remo_vinoth profile image
vinoth

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

Some comments may only be visible to logged-in visitors. Sign in to view all comments.