To learn iOS programming, you need to learn three different skills:
1. How to write commands in the Swift programming language
2. How to use Apple’s software frameworks
3. How to create user interfaces in Xcode
Essentially, every iOS app consists of three parts:
• Your code to make an app do something useful
• A user interface that you can design visually in Xcode
• Access to hardware features of an iOS device through one or more of Apple’s iOS frameworks
Swift Application Development Architecture
-->> top
Apple provides dozens of frameworks for iOS (and their other operating systems as well such as macOS, watchOS, and tvOS). By simply using Apple’s frameworks, you can accomplish common tasks by writing little code of your own. Some of Apple’s available frameworks include
• UIKit – iOS user interface and touch screen support
• ARKit – Augmented reality features
• Core Animation – Displays animation
• GameKit – Creates multiplayer interactive apps
• Contacts – Accesses the Contacts data on an iOS device
• SiriKit – Allows the use of voice commands through Siri
• AVKit – Allows playing of audio and video files
• MediaLibrary – Allows access to images, audio, and video stored on an iOS device
• CallKit – Provides voice calling features
Now you have xCode installed on your machine. Next, open Xcode from the Application folder and proceed after accepting the terms and conditions. If everything is fine, you will get the following screen –
Select Get started with a playground option and enter a name for playground and select iOS as platform. Finally, you will get the Playground window as follows:
Click Next
Following is the code taken from the default Swift 4 Playground Window.
When the above program gets translated, it should display the following result in Playground result area (Right Hand Side).
A variable provides us with named storage that our programs can manipulate. Each variable in Swift 4 has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.
Swift 4 supports the following basic types of variables −
• Int or UInt − This is used for whole numbers. More specifically, you can use Int32, Int64 to define 32 or 64 bit signed integer, whereas UInt32 or UInt64 to define 32 or 64 bit unsigned integer variables. For example, 42 and -23.
• Float − This is used to represent a 32-bit floating-point number. It is used to hold numbers with smaller decimal points. For example, 3.14159, 0.1, and -273.158.
• Double − This is used to represent a 64-bit floating-point number and used when floating-point values must be very large. For example 3.14159, 0.1, and -273.158.
• Bool − This represents a Boolean value which is either true or false.
• String − This is an ordered collection of characters. For example, "Hello, World!"
• Character − This is a single-character string literal. For example, "C"
Swift 4 also allows to define various other types of variables, which we will cover in subsequent chapters, such as Optional, Array, Dictionaries, Structures, and Classes.
The following section will cover how to declare and use various types of variables in Swift 4 programming.
Variable Declaration
A variable declaration tells the compiler where and how much to create the storage for the variable. Before you use variables, you must declare them using var keyword as follows −
The following example shows how to declare a variable in Swift 4 −
When we run the above program using playground, we get the following result −
Strings in Swift 4 are an ordered collection of characters, such as "Hello, World!" and they are represented by the Swift 4 data type String, which in turn represents a collection of values of Character type.
Create a String
You can create a String either by using a string literal or creating an instance of a String class as follows −
String interpolation is a way to construct a new String value from a mix of constants, variables, literals, and expressions by including their values inside a string literal.
Each item (variable or constant) that you insert into the string literal is wrapped in a pair of parentheses, prefixed by a backslash. Here is a simple example −
When the above code is compiled and executed, it produces the following result:
Swift 4 arrays are used to store ordered lists of values of the same type. Swift 4 puts strict checking which does not allow you to enter a wrong type in an array, even by mistake.
If you assign a created array to a variable, then it is always mutable, which means you can change it by adding, removing, or changing its items; but if you assign an array to a constant, then that array is immutable, and its size and contents cannot be changed.
Creating Arrays
You can create an empty array of a certain type using the following initializer syntax −
Here is the syntax to create an array of a given size a* and initialize it with a value:
You can use the following statement to create an empty array of Int type having 3 elements and the initial value as zero −
Following is one more example to create an array of three elements and assign three values to that array −
Swift 4 dictionaries are used to store unordered lists of values of the same type. Swift 4 puts strict checking which does not allow you to enter a wrong type in a dictionary even by mistake.
Swift 4 dictionaries use unique identifier known as a key to store a value which later can be referenced and looked up through the same key. Unlike items in an array, items in a dictionary do not have a specified order. You can use a dictionary when you need to look up values based on their identifiers.
A dictionary key can be either an integer or a string without a restriction, but it should be unique within a dictionary.
If you assign a created dictionary to a variable, then it is always mutable which means you can change it by adding, removing, or changing its items. But if you assign a dictionary to a constant, then that dictionary is immutable, and its size and contents cannot be changed.
Creating Dictionary
You can create an empty dictionary of a certain type using the following initializer syntax −
When the above code is compiled and executed, it produces the following result –
When the above code is compiled and executed, it produces the following result –
When the above code is compiled and executed, it produces the following result −
A function is a set of statements organized together to perform a specific task. A Swift 4 function can be as simple as a simple C function to as complex as an Objective C language function. It allows us to pass local and global parameter values inside the function calls.
• Function Declaration − tells the compiler about a function's name, return type, and parameters.
• Function Definition − It provides the actual body of the function.
Swift 4 functions contain parameter type and its return types.
Function Definition
In Swift 4, a function is defined by the "func" keyword. When a function is newly defined, it may take one or several values as input 'parameters' to the function and it will process the functions in the main body and pass back the values to the functions as output 'return types'.
Every function has a function name, which describes the task that the function performs. To use a function, you "call" that function with its name and pass input values (known as arguments) that match the types of the function's parameters. Function parameters are also called as 'tuples'.
A function's arguments must always be provided in the same order as the function's parameter list and the return values are followed by →.
Syntax
Take a look at the following code. The student’s name is declared as string datatype declared inside the function 'student' and when the function is called, it will return student’s name.
When we run the above program using playground, we get the following result −
When we run above program using playground, we get the following result −
Closures in Swift 4 are similar to that of self-contained functions organized as blocks and called anywhere like C and Objective C languages. Constants and variable references defined inside the functions are captured and stored in closures. Functions are considered as special cases of closures.
Syntax
Following is a generic syntax to define closure which accepts parameters and returns a data type −
When we run the program using playground, we get the following result −