1. Error Handling?
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 the
try?expression, the value of the expression is
2. Enum, Struct, Tuples?
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.
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.
For 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.
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.
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.
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.
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.
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.
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.