Hello Friends.
As we know classes are reference type in Swift programming. Here I only discuss classes as a data type and will explain more in Swift Object Oriented part.
In object-oriented programming, a class is an extensible program-code-template for creating objects, providing initial values for state (member variables) and implementations of behaviour (member functions, methods). (Wikipedia)
Before starting a code, I am describing what classes can do and what not in Swift.
- Swift class support only single level inheritance
- Swift class support Polymorphism
- Swift class support three Access modifiers internal, private and public.
- Swift class support class level methods and properties
- Swift class support instance level methods and properties
All above points will discuss with detail in Swift Object Oriented section.
As we mostly start Object Oriented Programming, we define a class Person. Same thing happen here 🙂 .
Object instantiation.
Instance method and calling.
Class level method and calling.
In most languages we use static keyword for class level methods and fields. But in swift we use the above image syntax to define a class level methods.
Getter and Setter Method.
In above image, mFirstName is an instance level field having getter and setter. Here only I am showing syntax of Getter and Setter method, otherwise we did not require these methods in this scenario, because we can access this field using dot operator.
Class level field.
Pass By Reference.
Basic’s of class complete. Now we are moving to struct and I will explain classes in more detail when we start Object Oriented Programming in Swift.
In Swift structures are value type. In first look structures and classes look like similar but there are some differences.
Similarities.
- Both have properties
- Both have methods
- Both have init methods
- Both use extensions and protocol (Will explain in next section)
Differences.
- Structure are not inheritable
- Structures cannot achieve polymorphic behaviour
- Structures cannot support De-initializer method classes can (Explain in OOP in Swift section)
Now Structure of a Person 🙂
As we know and will learn more in future about classes require init method but in struct they are optional.
As you see now we did not define init method. In this case swift automatically generate init method for us. This is called member-wise initializer.
If we want to initialize our struct without any argument then we need give default values to struct fields as shown below image.
We can define methods in struct as in classes.
Struct level method and calling.
As we see in class, we use the keyword class to make a method as class level. But in struct we use static keyword to make a method as struct level.
Pass by value.
As you see in above image. When we change p1 age field p2 age field not effected. But when we did same thing in classes our p2 age field also effected. So this is the one big difference between struct and classes. Structs are value type and classes are reference type.
One more important thing in Swift. Every data type which we use in Swift like Int, String, Float, Arrays, Dictionaries etc all are Structs 🙂 .
Hmmmmmmm. OK Guys we will meet in next tutorial Bye. 🙂
by
I possess examine a few good stuff the following. Undoubtedly price saving intended for revisiting. I actually surprise the way so much attempt you add for making this kind of fantastic informative internet site.
Hello I am so delighted I found your weblog, I really found you
by accident, while I was searching on Digg for something else, Regardless I am here now and
would just like to say thanks for a fantastic post and a all round entertaining blog (I also love the theme/design), I don’t have time to browse
it all at the moment but I have saved it and also included your RSS
feeds, so when I have time I will be back to read much more, Please do
keep up the superb work.
Thanks for every other informative site. Where else may I get that kind of info written in such an ideal method?
I’ve a mission that I’m just now working on, and I’ve been at the glance out for such information.
It’s an remarkable piece of writing in support of all the web
viewers; they will obtain advantage from it I am sure.