Author Archives: Harsh Thaker

About Harsh Thaker

Computer Vision Researcher and programmer who likes to create amazing projects and iOS apps. Also a Robotics enthusiast and love to write blogs.

mutating method swift simple example – how to write ?

mutating method swift simple example – how to write ?

Structure and enumeration are value type in swift. That means an instance method can not change its properties. For example a method within structure cannot change any variable defined within structure. To do so you need a method that is mutating – means changing the property or being able to change a property value.

Cannot use mutating member on immutable value – such error will arise if you would have instance with let keyword and try to call mutating function with it. Use var istead

self keyword swift simple example – what is use ?

Use of self keyword swift simple example

One of the use of “self” keyword in swift is when you want refer a property within structure, class or enum. We can understand by the following example:

when we call function and within its definition if there’s written x+2 then it will refer to the x passed in function call whereas if inside definition of the function we’ve written self.x then it refers to the x that is one of the structure property.

willSet didSet example swift – How to use ?

willSet didSet example swift

willSet and didSet are called property observers. They observe the changes in the value of a property. willSet observe what changes are made that means new value and didSet observe what change was made that is old value.

 

structure example swift programming

structure example swift programming

Here’s a simple structure example in swift. We are creating three structures. One with get and set methods. Later we are initializing their instances. How get and set method works is written in code.

 

Incrementer using nested function swift

Incrementer using nested function swift

Swift provides nice feature of nested function. Here, in this example when we create a constant newIncrementer which refers to makeIncrement function that creates a copy of makeIncrement function.

Amount we pass in it, is stored within that copy. copy of the total variable increases every time that function is called.

 

cannot convert return type int to return type ()->int

cannot convert return type int to return type ()->int

If you are working with nested functions and you face such error while returning a function then see if you haven’t used parentheses after function name after return.

 

Closures example swift – How to write ?

Closures example swift – How to write ?

Closure gives the feature to write an inline function block. Here. we have taken an example of array mapping.

Notice the “!” mark in dictionary subscript. It is used because a dictionary always returns an optional value making sure the case that a key might contain empty value. Here, number % 10 will always fetch a value from dictionary so we’ve used forced unwrapping.

Passing function within function parameter swift

Passing function within function parameter swift

This is one of the powerful feature of swift. That’s called “function type” that means function itself is a type and you can pass this function to another function. For example (Int,Int) -> Int that means you can pass any such function which takes two integer parameters and returns an integer.

Below is a great example done by me, would be easy to understand the concept.

So, in above program we have to similar type of functions. addTwoNum and mulTwoNum. Another function is testFunc – that takes two integers and a function as a parameter. Wonderful is that we passed first addTwoNum as parameter and then mulTwoNum and we got both results accordingly.

inout variables swift – how it works ?

inout variables swift – how it works ?

Swift function parameters are by default constant type. That means you can not change parameter value inside the function body. In such case, if you need to change values then use inout keyword before the parameters. To understand this, let’s see below example of swapping to values :

While calling the function parameters must be passed using “&” sign before them. It looks similar to pass by reference concept.

defer swift – what is defer in swift ?

defer swift – what is defer in swift ?

It’s similar to the finally{} black used in C++ and Java. That means the code in defer will run anyways no matter what.