10s of How to use …


1. Error Handling?

error_handling_swift

A safe net when something goes south. I do find this gives a better context in error handling and better readability.

What is try! and try?

Use try! when you need to ensure your app always succeed as in won’t throw an error, but when it really does, your app shouldn’t work.

try! before the expression to disable error propagation and wrap the call in a runtime assertion that no error will be thrown. If an error actually is thrown, you’ll get a runtime error.

try? to handle an error by converting it to an optional value. If an error is thrown while evaluating thetry? expression, the value of the expression is nil

from Apple

2. Enum, Struct, Tuples?

enum

For enum: This is only applicable for fixed values and size. Using enum will be neater than just using array (with pre-filled). You can detect compilation error if you happen to misspell any strings(to avoid runtime crash).

For structs: For my understanding, if you can create structs, try to avoid use class. Struct is safer for reference around and in multithreading environment thus you will be saved from memory leaks etc issues.

StackOverFlow discussion

For tuples:

tuples

A tuple is a group of zero or more values represented as one value.

For tuples, I find myself seldom use them but it can be very powerful in switch context.
Switch TupleFor tuple: The ” _ “ will ignore the first parameter (which is the name) and will only filter the age (1…10), thus, it will fall into the first condition.

For tuple2: Where is useful in filter the tuples and in this example, it filtered the tuples by name regardless the age.

For tuple3: Only (name = Apple, age = 20) will fall into the third condition.

Fallthrough: It is used to continue execution into the next case, regardless of whether that next case is satisfied or not. Sounds weird to me to continue into wrong condition but I guess it has its own purpose of use.

More on Tuples & Enums

For more details in Switch tuples

 

4.Let, var, _, weak, strong?

Let: The value cannot be changed once initialised.
Var: The value can be changed.
 __  : Use it when the element key name is not important.

 

5. Lazy, Mutating, dynamic, var (get)?

Lazy: It has to be var. Lazy is only being initialized once when being called. Memory will not be allocated if it is not being called once.

Based on the below example, “formatter initialized” is being printed out once when it is being initialised at the first time.

Lazy

Mutating: a

Dynamic:

 

6. DidSet? WillSet? Set? Get?

Set: Set the property value

Get: Get the property value

WillSet: Before property is set.

DidSet: After property is set.

Usually they are used as computed properties.

set_get

 

7. Typealias?

Do correct me if I’m wrong, typealias is used to increase readability. Read across articles online and people use it for closures and data types.

typealias

8. Map?

Map is helpful when you want to transform an array to your customised array. It saved more lines than “for” loop thus looks cleaner in this way.

Map

9. FlatMap?

For definition is to flatten nested array. It will also filter out nil as default.

Depending on what kind of structures you want, flatMap comes into handy when you need to restructure your data when you get it from server.

Flatmap
10. Reduce?

I took some to avoid reduce because at first sight it looks complicated but after some times, it isn’t! I use more Map, Flatmap but it is good to know Reduce as well.

Reduce is use to combine arrays of objects (strings, ints etc.) into ONE.

reduce

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s