2

I'm starting to learn Objective-C watching YouTube lectures. In one of the lectures, the teacher said that whenever you try to call methods of an object that do not exist in its public API, the compiler will warn you, but will let you do it. In addition, if the method really exists, it would work at runtime.

An example to illustrate:

I have two classes: Vehicle and Ship. Vehicle implements the method move and ship is a child of Vehicle and implements shoot.

In the lecture, it was said that this code would compile (with a warning):

Ship *s = [[Ship alloc] init];
Vehicle *v = s;
[v shoot];

And this code would, in fact, work. Since v is actually a ship.

But what is happing in practice is that the compiler does not allow me to compile this code, because shoot does not exist in Vehicle public API.

The compiler error: No visible @interface for "Vehicle" declares the selector shoot

I really would like to know if the teacher was wrong or if I can change the compiler behavior. Could some help me? Thanks!

Obs.: I know I can do it using (id), I'm really trying to learn more about Objective-C behavior and syntax here

Classes:

vehicle.h:

@import Foundation;

@interface Vehicle : NSObject

- (void) move;

@end

vehicle.m:

#import "Vehicle.h"

@implementation Vehicle

- (void) move {
NSLog(@"moving");
}

@end

ship.h:

#import "Vehicle.h"

@interface Ship : Vehicle

- (void) shoot;

@end

ship.m:

#import "Ship.h"

@implementation Ship

- (void) shoot {
NSLog(@"shooting");
}

@end
felipeek
  • 1,193
  • 2
  • 10
  • 31
  • 1
    What errors are you getting specifically? – tangrs Feb 15 '15 at 01:45
  • @tangrs The compiler generates the following error: No visible @ interface for "Vehicle" declares the selector shoot – felipeek Feb 15 '15 at 01:47
  • What does your code look like? Could it be that your compiler is treating warnings as errors? – tangrs Feb 15 '15 at 01:48
  • @tangrs My code is exactly as I described in the question. Just those three lines of code and the classes Vehicle and Boat. About the compiler, I'm getting both warnings and errors normally, I don't think this is the case unfortunately – felipeek Feb 15 '15 at 01:51
  • It's very incomplete. What do the classes look like? – tangrs Feb 15 '15 at 01:52
  • @tangrs I added the code to the question. Thanks for trying to help me – felipeek Feb 15 '15 at 01:59
  • Works fine on my end... Edit: no it doesn't really, see dasblinkenlight's answer. – tangrs Feb 15 '15 at 02:01

1 Answers1

3

Your instructor is right, but he forgot to mention something important: the semantic that he describes is pre-ARC.

When ARC has been introduced several years ago, the type checking by the compiler became more important, because it needed to know the exact signature to know how to manage the data going in and out of the call. See this question for a more detailed explanation of why this is happening.

If you switch ARC off, you would see the warning, but the code would compile.

Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523