I am learning Swfit and i start studied from below link
https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/
https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Initialization.html
I have question regarding Initialization
in swift
.
What i understand is as below
1] It works like a constructor for swift classes.
2] We have to initialize all properties in it.
3] We have to call init() method of super class.
4] Before calling init() we have to initialize each and every property.
5] We can use any member or method of super classes after initialize it.
On the bases of above 5 points I create a classes.
but having a problem because of point 3,4 and 5.
Classes
/*
AdminManagmentSystem
- Some class which will consume lots of memory during init
*/
class AdminManagmentSystem {
var adminKey : String
init(key:String)
{
self.adminKey = key;
println("Consume lots of memory");
}
}
/*
Person
- A base class which can create key.
*/
class Person {
// Some property which will user to create private key.
private var privateKey : String = "private"
init()
{
privateKey = "private";
}
// function which will calculate key (Comman for all person type)
private func calculatekey() -> NSString
{
return self.privateKey + " key";
}
}
/*
Admin
- A sub class which have object of AdminManagmentSystem
*/
class Admin : Person {
// Constant variable
let adminmanagmennt : AdminManagmentSystem
override init()
{
self.adminmanagmennt = AdminManagmentSystem(key: ""); // Line1 : Consume lots of memory
super.init(); // Line2 : its compalsurry to call super.init
var adminKey = super.calculatekey(); // Line3 : We can use any member or method of supper after callign init().
self.adminmanagmennt = AdminManagmentSystem(key: adminKey); // Line4 : Again consume lots of memory
}
}
Download project
https://www.dropbox.com/s/afohuxpxxkl5b3c/understandInheritance.zip?dl=0
Problem
In
Line1
and Line4
i have to create object of AdminManagmentSystem
which consume a lot memory.
Question
1] Why swift make it compulsory to initialize each and every property before calling super.init()?
2] If I make my property constant why swift allow me to initialize it more then once in init method ?
3] Why i must have to write override
keyword before init() ?