I know how to do it in java but im stubling in objetice-c
In java I would have an interface like this:
public interface Car {
public void startCar();
}
and the a class which would implement this interface:
public class SomeCarImpl implements Car {
public void startCar() {
System.out.println("starting the car...");
}
}
and now I would be able to this in my main class
public void MainClass {
public static void main(String [] args) {
Car myCar = new SomeCarImpl();
car.startCar();
}
}
Now here I’m getting in trouble in objective-c. The first two things are easily made with protocols but when I want to call it like this, nothing happens
//header
id <Car> *myCar;
//instance
myCar = [[SomeCarImpl alloc] init];
//calling and nothing happens
[myCar startCar];
I hope you can understand my problem…and help me 🙂
//edit here is the code
@interface SomeCarImpl:NSObject<Car>
@end
@implementation SomeCarImpl
-(void)startCar{
NSLog(@"run");
}
@end
@protocol Car <NSObject>
-(void)startCar;
@end
@interface DetailViewController:UIViewController<UISplitViewControllerDelegate> {
IBOutlet UIButton *runButton;
id<Car> myCar;
}
@property(strong, nonatomic)IBOutlet UIButton *runButton;
@property(strong, nonatomic)id<Car> myCar;
@end
and finally (detailViewController.myCar = [[SomeCarImpl]alloc]init] is called in the tableView beforehand)
-(IBAction)runButton:(id)sender {
[myCar startCar];
}
Your Java code would vaguely translate into something like this:
In your ObjC code, you need to remove the asterisk from this line:
Because the type
idis already a pointer (although I don’t think this is not the root of your problem).