CaptainProg
CaptainProg

Reputation: 5690

Incomplete implementation of class method - what is missing?

I have changed one of my instance methods to a class method so that it can be accessed from another class. The method is successfully called, but I have one warning on my code:

My class code looks like this:

//...

@implementation myViewController

#pragma mark - myMethod
+ (void)myMethod:(CustomUIView *)customView didSelectText:(NSString *)text
{
    //...
}

//...

In my class header file, I have the following:

#import "CustomUIView.h"

//...

@interface myViewController : CustomUIViewController <CustomUIViewDelegate>
{
    //...
}

//...

@end

I imagine I must be declaring the method in the wrong part of the header file, possibly due to the clause? Or I'm missing something else altogether. I've had a good look around the net and as far as I can tell I'm following protocol; perhaps there's something peculiar to my setup?

Edit: This is my protocol from my CustomUIView header file:

@class CustomUIView;

@protocol CustomUIViewDelegate <NSObject>

+ (void)myMethod:(CustomUIView *)customView didSelectText:(NSString *)text;
//...
@end

Upvotes: 0

Views: 315

Answers (2)

Abizern
Abizern

Reputation: 150615

Your changes don't make sense.

You can access instance methods from other classes - they don't have to be class methods. Class methods means they are methods that are implemented by the class - not an instance of the class.

Secondly, within your new class method, you are calling a method (otherMethod:) which as an instance method, i.e. one that is called by an object of the class. Since you are calling it at [[self Class] otherMethod:text] this is wrong as [self Class] is used to call class methods, not instance methods. You don't have a valid reference to an object of your class to send the message to.

To add:

You've implemented a method:

+ (void)myMethod:(CustomUIView *)customView didSelectTerm:(NSString *)text;

but your protocol expects:

+ (void)termTextClickedOn:(TSAlertView *)customView didSelectTerm:(NSString *)term;

The names you give your actual parameters don't matter so the difference in text and term don't count, but the method names, as written in Objective-C boil down to:

+ myMethod:didSelectTerm:

and

+ termTextClickedOn:didSelectTerm:

Not only are the two names different, but the types of the first parameters are different as well, one takes a CustomUIView *, the other takes a TSAlertView *, which might work if one is a subclass of the other, but in any case, your method names are wrong.

Upvotes: 0

Rad&#39;Val
Rad&#39;Val

Reputation: 9231

Your mistake is in the name of the method. Implementation is didSelectText and interface is didSelectTerm. (Text vs Term -> obviously should be the same) Also, you call [[self class] otherMethod:text]; as a class method, which, if you look closely, is not.

Upvotes: 1

Related Questions