0
class Model {
    let userId: Int
    let username: String
    private let description: String?

    init(userId: Int, username: String, description: String?) {
        self.userId = userId
        self.username = username
        self.description = description
    }
}

class ModelDetail: Model {
    let url: String
    let detail: String
    
    init(url: String, detail: String, userId: Int, username: String, description: String?) {
        self.url = url
        self.detail = detail
        
        super.init(userId: userId, username: username, description: description)
    }
}

I prepared this sample code to copy here. I don't want to inherit "description" value from Model class. When I try to something I am getting this error: Missing argument for parameter 'description' in call

Is it possible not to inherit some variable?

Chris
  • 27
  • 4
  • Does this answer your question? [What is a good example to differentiate between fileprivate and private in Swift3](https://stackoverflow.com/questions/39027250/what-is-a-good-example-to-differentiate-between-fileprivate-and-private-in-swift) – Dalija Prasnikar Jan 24 '21 at 19:40
  • Your `description` property is optional so why just not ignore it? – Joakim Danielson Jan 24 '21 at 21:02

1 Answers1

1

You cannot not inherit description, but you can assign a value nil to it, and simply ignore it.

If you can modify Model, you can change its init to

class Model {
    // ...

    init(userId: Int, username: String, description: String? = nil) 
    // ...
}

Since description is optional, we set it to nil by default, so ModelDetail doesn't need to set it at all:

class ModelDetail: Model {
    let url: String
    let detail: String

    init(url: String, detail: String, userId: Int, username: String) {
        self.url = url
        self.detail = detail

        super.init(userId: userId, username: username)
    }
}

If you cannot change Model, then ModelDetail can invoke Model's init with nil for description:

class ModelDetail: Model {
    // ...

    init(url: String, detail: String, userId: Int, username: String) {
        // ...

        super.init(userId: userId, username: username, description: nil)
    }
}

If you want to strictly adhere to the inheritance rules (which do not allow for "unused" fields), then you need to separate description from the model ModelDetail will be inheriting from. I.e.:

  • Have a base model, which only has userId and username
  • Both Model and ModelDetail will inherit from that base model, rather than each other:
class BasicModel {
    let userId: Int
    let username: String

    init(userId: Int, username: String) {
        self.userId = userId
        self.username = username
    }
}

class Model: BasicModel {

    private let description: String?

    init(userId: Int, username: String, description: String?) {
        self.description = description
        super.init(userId: userId, username: username)
    }
}

class ModelDetail: BasicModel {
    let url: String
    let detail: String

    init(url: String, detail: String, userId: Int, username: String) {
        self.url = url
        self.detail = detail

        super.init(userId: userId, username: username)
    }
}
timbre timbre
  • 12,648
  • 10
  • 46
  • 77
  • Thank you. Our api is returning extra value in child model. I used a separate struct for each. I asked to fix the structure, but it seems that I need to think carefully. – Chris Jan 25 '21 at 05:59