EXEMPLARS – Simplify Your Landscape

Swift 101| Things Every Beginner Should Know!

Swift is a general-purpose, multi-paradigm programming language developed by Apple Corporation for the products catering to its ecosystem. These include iOS, macOS, Linux, and z/OS platforms. It is designed to run along Apple’s Cocoa and Cocoa Touch frameworks, and the large body of existing Objective-C code written for Apple products. It is built using the open-source LLVM compiler framework and has been included in Xcode since version 6. It utilizes the Objective-C runtime library which allows C, C++, Objective-C, and Swift code to run within one program. Swift is exceptionally safe, and interactive and combines the best in modern language thinking, with wisdom from the broader Apple engineering culture and copious contributions from its open-source community. If you wish to venture into the domain of App Development, then these series of posts could serve as a holy grail for you. Go through it sincerely, and by the end of it, you’ll acquire a clear and comprehensive understanding of the concepts of Swift. Here, we are assuming that you are already adept with the concepts of Object Oriented Programming, if not then visit this link and make sure that you have Xcode preinstalled on your Mac. Variable and constant In the entire project life cycle, we need to stock a lot of information. Some data needs to be altered while sometimes it needs to be as is. To achieve this, we have Variables & Constants. A variable is a designated memory location which temporarily stores data and can acquire different values while the program is running. One can declare a variable by using the ‘var’ keyword. Variables in Swift Example A constant is a designated memory location that briefly stores data and remains the same throughout the execution of the program. If you want to retain a particular value – declare them as constant. If you try and change the value of constant, Xcode will show an error message and won’t proceed further. This helps in code optimization and makes your code run faster. Constant in Swift is declared by using ‘let’ keyword, as was applied in elementary Mathematics. Constant in Swift Example NOTE: It is important to note that we must ALWAYS assign a unique name for constants and variables to avoid a compilation error. Data Types We have different classifications of data in general, like name, phone number, weight, etc. Now how to represent them in a project using the language of computers? Here comes into play — Data types. In computer science, including computer programming, a data type or simply type is an attribute of data which tells the compiler and the interpreter how the programmer intends to utilize the data. i) String: As the name suggests, we can store a sequence of characters in this data type. For example: Any sentence: “How are you?” , “This is a really nice blog.” Syntax: import UIKit let constant : String = “Mitesh” print (constant) ii) Int: Int is used to store the whole number, where Int32, Int64 is used to store 32 or 64 bit signed integer and UInt32, UInt64 is used to store bit unsigned integer For example: Any whole numeric number: 42, 100, 999 Syntax: var int = 42 print (int) iii) Float: This data type is applied to store a 32-Bit floating point number. They are used to store the fractional component. For example: Any fractional value: 1.343555 Syntax: var float = 1.343555 print(float) (iv) Double: This data type is employed to store 64-bit Floating point number. They are also employed to store the fractional component. Double has better accuracy than float. Any fractional value: 1.34315 Syntax: var double = 1.24315 print (double) v) Bool: Bool stores a Boolean value which can either be true or false. Use this when you require a firm outcome. For example: Do you want to receive push notifications? Do you want to allow the user to see your mobile number? Syntax: var boolVariable = false boolVariable = true Here are the limits of the data type. Type    Typical Bit Width        Typical Range Int8      1byte   -127 to 127 UInt8   1byte   0 to 255 Int32    4bytes  -2147483648 to 2147483647 UInt32 4bytes  0 to 4294967295 Int64    8bytes  -9223372036854775808 to 9223372036854775807 UInt64 8bytes  0 to 18446744073709551615 Float    4bytes  1.2E-38 to 3.4E+38 (~6 digits) Double 8bytes  2.3E-308 to 1.7E+308 (~15 digits) iv) Dictionary: Dictionary is employed to store an unsorted list of values of the same Swift 4 puts strict checking which does not allow you to enter a wrong type in a dictionary even by error. Dictionary in Swift Dictionary in Swiftv) Array: Arrays are used to store lists of values of similar type. Swift 4 puts strict checking which does not allow you to enter a wrong type in an array, even by mistake. All the objects of an array ought to be of the same data type, i.e., int string, dictionary, double, etc. Conditional statements Sometimes we wish to execute code if a specific condition is valid. It is represented primarily by the if and else statements. You give Swift a condition to check, then a block of code to execute if that condition is found true. Loop When it comes to performing a repetitive task, it is not feasible to type the same code again and again; instead, one can use loops. Loops are simple programming constructs that repeat a block of code for as long as a condition is deemed true. Example: a) For loop for i in0..<5 { print(i) } b) While loop var index =10 while index <20{ print(“Value of index is \(index)”) index = index +1 } Function Functions are self-contained blocks of code that perform a specific task. You can give a function a name that identifies what it does, and this name is used to call the function to perform its task when needed. Every function in Swift has a particular type, consisting of the function’s parameter type and return type. Example: a) Function definition without parameter and return type: This method is not