Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to create a default instance object of custom class with SwiftUI

I have a class ScanItem defined as follows:

class ScanItem {
    let title: String
    let content: String

    init(title: String, content: String) {
        self.title = title
        self.content = content
    }
}

I often need a default object of this class. I know I can pass default value to each variable of the class, but my question is: is there a mean to create default instance of this class, that could be used like this:

let newScan = ScanItem.defaultInstance

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Create a static property that returns a default instance

extension ScanItem {
    static var defaultInstance {
        let item = ScanItem()
        item.title = "<default title>"
        item.content = "<default content>"
        
        return item
    }
)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading