문제

I have the following code,

In .h file:

@interface MyBaseClass : NSObject
{
    - (void)myMethod;
}

In .m file:

@implementation MyClass2000 : MyBaseClass
{
    - (void) myMethod
    {
        //Does something interesting here
        myData[0] = 0;
        myData[1] = 1;
    }
}

Later I created a class that extends "MyClass2000" and is called "MyClass2001"

.h file:

#import "MyClass2000.h"

@interface MyClass2001 : MyClass2000
@end

.m file:

@implementation MyClass2001 : MyClass2000
{
}

This class is exactly the same as "MyClass2000" (but I specifically need to keep them in separate classes, and one extending the other. I also need a "MyClass2002 class", which in this case will need to update only 1 of the indexes, so I tried to call [super myMethod] as follows:

The next class extends "MyClass2001"

.h file:

#import "MyClass2001.h"

@interface MyClass2002 : MyClass2001
@end

.m file:

@implementation MyClass2002 : MyClass2001
{
    - (void) myMethod
    {
        [super myMethod];

        //Only update one value
        myData[1] = 1;
    }
}

On that line I'm getting: No visible @interface for 'MyClass2001' declares the selector 'myMethod'.

Any tips on how to get this to work?

Thanks!

도움이 되었습니까?

해결책

That isn't how you declare a method in Objective-C. In your interface, you declare the method like this:

@interface MyClass2000 : MyBaseClass {
    NSString *someVariableDeclaration;
}

- (void) myMethod;

@end

And then you implement the function in your .m like this:

@implementation MyClass2000 {
    NSString * someDifferentVariableDeclaration;
}
 - (void) myMethod
{
    //Does something interesting here
    myData[0] = 0;  
    myData[1] = 1;
}
@end

다른 팁

Where you have the line:

[super MyClass2001];

Instead you want to have:

[super myMethod];

You were trying to call a method named MyClass2001, which doesn't exist.

1.Jus check the Inheritance hierarchy. In Objc its necessary to have implementation for every interfaces. But your code tells you don't have MyBaseClass implementation.

2.Method declaration cant be done inside {} in .h.

3.Method definition cant be done inside {} in .m.

4.Implementation does not extended only interface be extended.

It should work if proper hierarchy maintained.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top