0

I have the following code:

class MySuperClass : UIViewController { var model: ModelA! } class ModelA { var aBool = true } class ModelB: ModelA { var boolBelongsToB = true } class MySubclass: MySuperclass { func testFunction() { let theBool = (model as! ModelB).boolBelongsToB // Simplify this } } var aSubclass = MySubclass() var aModelB = ModelB() aSubclass.model = aModelB 

What I want to do is simplify having to use the code (model as! ModelB) everytime I want to access my model in MySubclass. How can I do this?

2
  • 2
    This is a bit confusing, could you please provide more code so we can get a better understanding the relation between each class. Also, are you using the same variable name? Commented Oct 24, 2017 at 14:56
  • @valcanaia Done Commented Oct 24, 2017 at 15:04

1 Answer 1

2

Why not just create a computed property in your subclass Y which returns the correct type. Like

var modelB: ModelB {get { return model as! ModelB }} 

Instead of getting it then as "(model as! ModelB)" you can just get it as "modelB".

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.