Learn Swift Together / Some Useful Codes And Optional
Hello everyone, in this article we will talk about useful functions in swift programming language. Then we will talk about the optional feature of the swift language and end the article. If you’re ready, let’s start !
If you are Beginner for programming, this article maybe not for you.
Learn about this series of articles.
Some useful structures
Generate a random value
If we want generate a random value in swift programming language, we can easily do this with random functions of classes such as Int, Double and Float.
Example :
Mathematical Operations
With the Swift language, you may want to round a decimal number and perform operations with exponents or square roots. The code below contains mathematical operations and explanations that may be useful to you.
Example :
Date Operations
Date operations in programming are used in almost every project. In this section, we will see operations such as taking instantaneous time and separating it into its components.
Example :
Operations with Units of Measure
You can define units of measurement such as meters and kilometers that we use in our daily lives and perform conversions among themselves. If you want to see all units, you can visit https://developer.apple.com/documentation/foundation/units_and_measurement.
Optional
There is a possibility that some variables may be null in the programs we develop. We indicate that care should be taken by placing a (?) sign next to such variables. In this way, we can prevent crashes in runtime.
If we put (!) at the end of a variable specified as Optional, we guarantee that the variable will not be null. Thus, we remove the expression from being optional. It is worth remembering that if we do this even though the variable is null, the program will crash.
Take care and see you in the next article :)