0

I am recently developing an iphone app and I feel using inheritance makes the structure more sense and elegant. However, I am struggling with how does inheritance work in objc. The inheritance convention in Java does not seem work in here.

Circumstances:

It might be a bit complex but this looks the best way to describe the situation. Let's say I have 2 classes, A and B. And they look like the following:

// Class A
@interface A: B
@end

@implementation A
- (id)init{
    self = [super init];
    if(self){
    }
    return self;
}
@end

// Class B
@interface B: NSObject
@property A *littleA;

- (void)doSomething;
- (void)blabla;
@end

@implementation B
- (id) init {
    self = [super init];

    if(self) {
        _littlea = [[A alloc] init]; // error: No known class method for selector 'alloc'
    }
    return self;
}

- (void)doSomething{
    NSLog(@"something");
}

- (void)blabla{
    [littleA doSomething]; // error: No visible @interface for 'A' declares the selector 'doSomething:'
}

@end

And outside of these classes, there is a class C. In class B, I passed 'littleA' to Class C's method. In that method, I did:

[littleA isEqual:NULL];// error: No visible @interface for 'A' declares the selector 

I am utterly confused by these error messages.. Is this what the inheritance suppose to do? In Java, A extends B, B extends C, then I can call from A: a.b's method or a.c's method.

What did I do wrong? Anybody could explain the three error messages would be a great help!

  • Is this a copy-paste of your code? If so, don't you also have a warning about ["interface type cannot be statically allocated"](https://stackoverflow.com/questions/8460340/interface-type-cannot-be-statically-allocated/8460375#8460375)? – jscs Mar 01 '18 at 21:37
  • Hint: `@property A* littleA;` (note the pointer) Objective-C objects cannot be "statically" allocated. In other words, you can never have a variable that contains the entire object; you can only declare a variable that contains a reference to an object. – James Bucanek Mar 01 '18 at 21:40
  • @Josh Caswell and James Bucanek thanks for pointing that out. Unfortunately, it's my typo in the question.. I had the * for all my object properties. I have fixed the code in the question. But it's good to know the statically allocating objects. I only know the difference between pointers and reference, not how are they allocated :) – Mengchen Lin Mar 01 '18 at 22:36
  • 1
    You do not get an error on the very first line `@interface A: B`? Moreover, are you aware, that `A` is the subclass, not `B`? Likely you should rename the classes in your sample code to `BaseClass` and `Subclass` to make it more readable. This code is not your real code and likely you forgot to import `A`. – Amin Negm-Awad Mar 02 '18 at 14:22

1 Answers1

-1

In class B, your declaration of property littleA is missing the asterisk (which in Objective-C indicates that the variable is an object).

@property A *littleA;

I think this will clear up the errors you've posted.

Joshua Kaden
  • 1,210
  • 11
  • 16
  • Sorry my typo. There is a * sign in the code. I have added it in the question. But this seems not the cause of the errors... – Mengchen Lin Mar 01 '18 at 22:38