Class has not initialiser error in Swift – Solved.

By | August 16, 2017

Apart from other languages, when you write a class in Swift, It should have a initialiser.
To solve this we have to write init() function in that particular class. Just like below.

For example, look at the below class.
Here we have 5 variables.


The above error usually comes when you don’t initialize all variables in the class in init method.

Example

import Foundation

class SubCategory {
  
    var subCatId : String;
    var subCatName : String;
    var subCatImage : String;
    var catId : String
    var subCatActive : String
    
    init(SubCatId subCatId : String, SubCatName subCatName : String, SubCatImage subCatImage : String, SubCatActive subCatActive : String, CatId catId:String) {
        //  Initialize all member variables here...
        self.subCatId = subCatId;
        self.subCatName = subCatName
        self.subCatImage = subCatImage
        self.catId = catId;
        self.subCatActive = subCatActive;
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *