Re: A Question about [[self superview] doSomething]
Re: A Question about [[self superview] doSomething]
- Subject: Re: A Question about [[self superview] doSomething]
- From: Shawn Erickson <email@hidden>
- Date: Tue, 26 Apr 2005 10:22:01 -0700
On Apr 26, 2005, at 10:00 AM, Brian O'Brien wrote:
I have manage to get this to work for me until doSomething needs
parameters, then the application crashes when the method is called.
Also I'm getting a warning `NSView' may not responde to
`-doSomething:'
can not find method `-doSomething:'; return type `id' assumed
my superview in this case is a subclass of NSView and has a method
called doSomething.
Which works fine if no parameters are passed..
It would be best to show some code snippets or we will only be able to
guess and what does the crash look like?
Anyway the compiler warnings are telling you that in code you are
attempting to send a message to an instance of NSView that NSView isn't
known to support (based on the type for the return from -superview). It
also cannot find a definition for it so it cannot tell the return type
that is expected for -doSomething:.
If your subclass supports the doSomething: message and you know you
will be getting it you may want to type cast what you get back from
[self superview] to MySubclass*. In other words something like...
[(MyNSViewSubclass*)[self superview] doSomething:blah];
Other options exist such as an informal protocol which in this case
could be a simple category against NSView to tell the compiler that
NSView instances may support the message.
The following is from...
<http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/
LanguageOverview/chapter_3_section_7.html#//apple_ref/doc/uid/20001424/
BAJJABHJ>
Informal Protocols
The simplest way of declaring a protocol is to group the methods in a
category declaration:
@interface NSObject ( RefCounting )
- (int)refCount;
- incrementCount;
- decrementCount;
@end
Informal protocols are typically declared as categories of the NSObject
class, since that broadly associates the method names with any class
that inherits from NSObject. Because all classes inherit from the root
class, the methods aren’t restricted to any part of the inheritance
hierarchy. (It would also be possible to declare an informal protocol
as a category of another class to limit it to a certain branch of the
inheritance hierarchy, but there is little reason to do so.)
When used to declare a protocol, a category interface doesn’t have a
corresponding implementation. Instead, classes that implement the
protocol declare the methods again in their own interface files and
define them along with other methods in their implementation files.
An informal protocol bends the rules of category declarations to list a
group of methods but not associate them with any particular class or
implementation.
Being informal, protocols declared in categories don’t receive much
language support. There’s no type checking at compile time nor a check
at runtime to see whether an object conforms to the protocol. To get
these benefits, you must use a formal protocol. An informal protocol is
good for times when implementing all the methods is optional, such as
for a delegate.
-Shawn
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden