开发者

Dynamic include classes OR Instantiate objects without including class

开发者 https://www.devze.com 2023-02-20 03:06 出处:网络
Basically I have a load of classes which are all subviews of UIView. The app is a (sort of) slideshow application. I want to be able to make lists of potential \"routes\" that the slides could take i

Basically I have a load of classes which are all subviews of UIView.

The app is a (sort of) slideshow application. I want to be able to make lists of potential "routes" that the slides could take in plists.

As such I may have 100s of classes and I don't want to have to include all of them in the potential that I may use one of them once.

So is there a way around this? Can I instantiate a class without including it somehow?

Or am I going to have to include every potential class I'm going to use?

Is ther开发者_如何学Goe a "global include" like include all... clutching at straws here. :p

Thanks Tom


Why would you have hundreds of classes? It sounds like you have roughly one custom class, which would represent a slide.

Your comment on the question helps. Consider separating the slide from its content. You could have a single slide class which provides the functionality common to all slides, including the ability to manage one or more content objects. Then you'd create a bunch of different content classes: spreadsheet, animatable graph, checklist, whatever. A slide would look at its data to figure out which content class to instantiate.

If you have a common base class for all your content classes, the slide class only needs to know about the base class. The content base class could act as the manager for all the content classes. You could give it a factory method like -contentForData:(NSData*)data that returns an appropriate content object for the given data. The slide class doesn't need to know about anything more than the content base class then, so this sort of accomplishes your goal of instantiating your content classes without having to include all their headers in your slide class. The content base class would, of course, still have to know about all the content classes.

It gets a little tricky with the base class needing to know about its subclasses but the subclasses being derived from the base class. I think you can get around this by using a forward @class declaration in the content subclasses.


If you're using the class, you must know what set of messages it responds to. Factor out that information into either a common superclass or a protocol, and then you only need to use that.


Can I instantiate a class without including it somehow?

I guess this is not possible because compiler should definitely see class definition in the current scope before using a variable of class type.


IF I understand your question correctly, you want a single place to put your #include directives so that every class in your project has access to them. You can do this in the ProjectName_Prefix.pch file. Any #include or #import statements there will be included in every source code file.

0

精彩评论

暂无评论...
验证码 换一张
取 消