protocol vs. subclassing NSView
protocol vs. subclassing NSView
- Subject: protocol vs. subclassing NSView
- From: Development Staff <email@hidden>
- Date: Sat, 21 Feb 2009 20:48:32 -0500
So I've got this app that uses various NSView subclasses (NSTextView,
NSImageView, QTMovieView, etc.) to display different kinds of data in
a window. The problem is that, when I need to select which kind of
view to use for the current data, I have very different code for each
kind of view (detecting what view handles the current data and
initializing the view), and it currently makes for a messy bit of code.
What I'd like to do is have an array of candidate views and a
consistent way to test against the current data and initialize the
view once I have selected a candidate. I'm trying to decide between a
formal protocol with -(BOOL)canDisplayData:(id)data and -
(int)setupDisplayInFrame:(NSRect)rect withData:(id)data methods, or a
sub-class of NSView from which all candidate view classes must descend.
I'm currently coding against the protocol, but reading the Cocoa Views
Guide to see what kind of work I'll need to put in to correctly wrap
the actual candidate views. If all I really need to do is pass on
calls to -drawRect calls to the actual view, then I feel comfortable
going with the protocol. If there is a lot more to it, then putting
all that work in a separate base class seems like the right thing to do.
I guess I'm just feeling a bit befuddled. I think I settled on the
protocol partly out of giddiness over the chance to use a new language
feature (I'm still a novice with Objective-C and the Cocoa frameworks,
though I've been dabbling with them for a few years now), so I'm just
wondering if other people think this sounds like a good solution, or
if I would go with the more traditional inheritance-based approach?
Jeffrey Dutky
Senior Software Engineer
Medhavi Software Inc.
email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden