Sunday, October 14, 2018

Tips to increase skill for iOS developer

Introduce

Hello everyone Swift is a new programming language developed by Apple Inc. for the purpose of supporting developers in developing applications that run on iOS, OSX, and watchOS. Swift is based on the inheritance of the C and Objective-C languages.
Today I would like to share some tips to improve iOS-Swift code.

Begin

Subscripts:

Class, struct and enum can define subscript. Subscripts can be used to quickly access collections, lists, or sequences. You can use subscripts to set and retrieve data with the index, but not through other methods. For example, you access elements in an array through someArray [index] The following are two examples for using subscript to make it clearer:

Do not use subscripts:


Use subscript

Function vs Computed Property

Function (or 💩 Code)

Here we will take a simple example of two functions to calculate radius for diameter and vice versa

Computed Property (or ❤ Code)

Computed Property offers GET and SET. I will do the above example using the computed property

Extension

Extension is used to add new functions to the class, struct, enum or protocol. We can extend the types without having to access the original source code. Here is an example of extention for Double to provide basic types of spacing

Use extension


Do not use extension (bad code)


Ternary conditional

This operator is a special type consisting of 3 parts with a pattern  question ? answer1 : answer2This is the shortcut used to assess 1 2 courtesy based formula  question is  true or  falseHere is an example to change the value of  rowHeight based on whether  header or not.

Use Ternary conditional

Do not use Ternary conditional (or 💩Code)

Nil coalescing

The nil coalescing operator has the form  a ?? bIt will unwraps an optional a if a is valid and if a is nil then the default value returned is b. Said it seems difficult to understand, so I will give an example: Assign the value  colorNameToUse as  userDefinedColorName if  userDefinedColorName valid. If it  userDefinedColorName is nil then the value will be set defaultColorName

Use Nil coalescing (or 😎 Code)

Do not use Nil coalescing (or 💩Code)

Optional Unwrapping (if let vs guard let)

Both boys  if let and  guard let girls can unwrap optional. However, let's guard better when we need to check nested conditions. It will come out if you meet a conditioner that does not meet. Further  guard let making its source code easier to read and easily maintain later. Here is an example to create a new user:

Use if let (or Bad Code)

Sử dụng guard let (or Awesome Code)

Generics

Generics are one of Swift's powerful features. Allows us to write flexible and easy reusable functions of any kind. Here is an example of how to write a function to swap two integers and two characters. Normally, we will write two functions for the two functions:

Use Generics (or 👊 Code)

We only need to create a single function with any type of data

Type safe using Enum

Enum defines a generic type for a set of related values ​​and allows us to work on it securely.

Do not use Enum

Sử dụng enum(or 👊 Code)

Using the enum makes the code clearer and clearer
So we went through some TIPs to improve our skills. Doing as many wrongs as possible will draw experience. No one but yourself pro. So everyone who shares more can leave behind comments so that those who do not know can know more. Thanks everyone.

No comments:

Post a Comment