1

In my program, I have a class called Object, which inherits from UIView. I also have other objects called: circle, square, triangle which inherit from Object.

My problem is that I would like to make a possible object called Image, which also inherits from Object. I would like all objects on the screen to inherit from Object, and then pass an NSArray pointer to my physics functions that contains all objects on the screen.

What is the best way to have my Image object acquire all the properties of my Object class?

Should I just create new properties for my Image object, or is there a way to inherit Object and still be able to display an image, since Object inherits from UIView and not UIImageView.

3 Answers 3

2

You've realized that a class cannot extend two parent classes.

Could your "Object" be a Protocol instead of a Class? That way circle, square and image could all implement that protocol while extending either UIView or UIImageView as needed?

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

1 Comment

This is a good idea as well. The question to ask is: Does your Object class need to store instance variables? If Object only provides methods to call, then it can be made into a protocol. If it has to store data with each Object, then it can not be a protocol.
1

That's a very…unusual way to do it. Have you had any Java experience?

What are you trying to do with the Shapes? Are you trying to draw them in those respective classes? Here, you're blurring the lines of MVC. From what it seems like, your Circle, Square, etc. classes are both a model and a view, which shouldn't happen. If you're trying to draw the shapes, you wouldn't create classes for them—you'd use the Quartz drawing methods to draw them in your view controller's view. If you're trying to store info about those shapes, you wouldn't want them to inherit from UIView. NSObject (or a custom Shape class) would be a better option. (It's also a bad idea to call the class "Object"—it could get confused with NSObject).

You're second paragraph is where my first question came from. Your NSArray does not have to contain variables of the same type—this is very different from Java's arrays (and other languages such as C# and C). Therefore, unless there were some methods or ivars you wanted to inherit, there was no reason to declare the Object class.

As for displaying images, what are you intending to do with Image? If you just want to display images (a view), then make it a subclass of UIImageView (which is a subclass of UIView). If you want do store information about images, make it a subclass of Shape (or in this case, your Object). However, you really can't do what you want to (inherit from both Object and UIImageView) because you shouldn't combine functionality of views and models.

Really, your best option here is to revisit your classes, make them adhere to MVC, and rethink your inheritance chain. Hope this helps!

Comments

1

The simplest thing would be to have Image inherit from Object and then have Image own a subview of type UIImageView.

1 Comment

This is what I'm looking for, it didn't cross my mind to do it like this, thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.