As promised,here is my second part of tutorial on learning Swift programming language.
In the previous article, we have discussed the basics of Swift programming language along with declaration of variables and constants in Swift.
Are you familiar with classes,objects and methods?.
I recommend you first grab some concepts on these topics before continuing this article.
The following code is an example of defining a Swift class:
If you want to extend a superclass,you should add a colon followed by the name of the superclass.As per the above syntax,you have to enclose the beginning and end of the class wth opening and closing curly braces.
In Swift project, classes are defined in a single .swift source code file.But in case of Objective-C,the classes are defined in two separate files, a header (.h) file and an implementation (.m) file.
No importance of Asterisk
Asterisk was used in Objective C to identify that a variable holds a pointer to an object.
In case of Swift programming, a constant can hold a pointer to an address,but you will no longer need to use asterisk here.
How to Declare Simple Properties in Swift ?
Here I have put an example to show declaration of properties in Swift:
In case of variables,you don't have to put the property type,if you assign an initial value to it.
Let me clarify that you can also initialize the properties from the initializer methods as mentioned in section below.
Here is another example of declaring a property without specifying a type
The properties of objective C had backing instance variables in which the actual value of the property is stored.But Swift comes represents properties by unifying the concept of properties and instance variables.There is no backing instance variable for property.You can declare everything about a property in a single location.
There is no use of self keyword in Swift to refer to a property unless the method you are writing code has a parameter with the same name as the property.
How to Declare Computed Properties in Swift ?
In the code below,we will see computed properties of Swift associated with get and set methods.
What is a computed Property in Swift ?
A computed property is just that: A computed value( degreeCelsius ) from degreeFahrenheit as shown below. There is no instance variable where the properties value is stored, you cannot change the "computed property itself".
In this example, the TempConverter class has two simple properties.
1) A simple degreeFahrenheit property
2) A computed degreeCelsius property.
In the example below, the get and set methods of the TempConverter class do not reference an instance variable, because there is no such thing as an instance variable in Swift!
On the other hand,set method uses the value passed to it, converts it to Fahrenheit and then stores the result in the degreeFahrenheit property.
newDegreeval is an implicitly named parameter holds the value passed to set method
You can also use a different parameter specifying the name in parentheses as shown below.
Functions associated with particular types are referred to as Methods. In case of Objective C, Classes are basically used to define methods, on the other hand Swift language offers user flexibility to have methods for Classes, Structures and Enumerations.
In Swift language, Classes, Structures and Enumeration instances are accessed through the instance methods.
Instance methods provide functionality.
1)To access and modify instance properties.
2)Functionality related to the instance's need.
To declare a method, you first specify the func keyword, followed by the name of the method.
If the method has parameters, you should enclose them inside the parentheses.
For each parameter, you specify the name of the parameter followed by a colon, followed by its datatype. If the method has a return value, you then add a hyphen and greater than sign (->) followed by the type of the return value.
The following code contains an example of a method that accepts no parameters and returns nothing:
Working With Initializer Methods in Swift
The following code contains an example of an initializer method that is used to initialize a class.
In the above example, the init method takes a previousDegreeTemp as parameter in which you can pass the previous temperature. This value is then stored in the degreeFahrenheit property.
You can create multiple initalizer methods as shown in the following code sample:
Here we have the unique method names as init(fromDegreeF:) andinit(fromDegreeC:) respectively.
In Swift, init methods are not inherited. In fact, what I mean to say,you shouldn't place any code in an init method that you want to be able to override in a subclass. If you have code that you want executed during the init phase of a class and want to be able to override that code in a subclass, you can create a separate method, place the initialization code in that method, and then call that method from the init.
How to Instantiate a Class in Swift ?
Here is an example of how you can create instance of a class
var Result=Result ()
As per the above code snippet, to create an instance of a class, you declare a variable to hold a reference to the class, type an equal sign (the assignment operator) and then the name of the class followed by parentheses.
Swift Interview Questions And Answers part 1
An Introduction To The Swift Programming Language
Top 5 Frequently Asked iOS Interview Questions And Answers.
Top Interview Questions And Answers in Objective C
Most Important iPhone interview Questions and Answers
Core Data Interview Questions and Answers