The expected identifier or '(' before @interface

First post, and I really hope this is not a recurring or resolved issue. I tried to find Google here, and although I found similar errors, Expected identifier or '(' , none of the solutions work for me.

Basically I try to learn design patterns, and since I knew a little java, I try to use it as an opportunity to learn objective-c, so I have a working Java program and an xCode project that I will get Expected identifier or '(' in my header file just before @interface

this is my java solution (very simple i know):

 public class Duck { public void quack(){ System.out.print("Quack!"); } public void swim(){ System.out.print("swimming duck!"); } public void display(){ quack(); swim(); } } public class mainClass { public static void main(String[] args){ Duck duck = new Duck(); duck.display(); } } 

and this is my version of objective-c.

 //duck.h #include <CoreFoundation/CoreFoundation.h> @interface Duck : NSObject{ //Expected identifier or '(' } @end // Duck.m #include "Duck.h" @implementation Duck -(void)quack{ printf("Quack!"); } -(void)swim{ printf("swimming duck!"); } -(void)display{ [self quack]; [self swim]; } @end // main.c #include <CoreFoundation/CoreFoundation.h> #include "Duck.m" int main(int argc, const char * argv[]) { Duck *duck = [[Duck alloc] init]; [duck display]; return 0; } 

If anyone can help, I would really appreciate it, and again sorry if this is a duplicate of the message

+4
source share
4 answers
 //duck.h //#include <CoreFoundation/CoreFoundation.h> #import <Foundation/Foundation.h> // or Cocoa/Cocoa.h @interface Duck : NSObject//{ //Expected identifier or '(' //} not necessary if there are no instance fields - (void)quack; - (void)swim; - (void)display; @end // Duck.m //#include "Duck.h" #import "Duck.h" @implementation Duck -(void)quack{ printf("Quack!"); } -(void)swim{ printf("swimming duck!"); } -(void)display{ [self quack]; [self swim]; } @end // main.c SHOULD BE ~main.m~ if using ObjC!!! //#include <CoreFoundation/CoreFoundation.h> //#include "Duck.m" #import "Duck.h" 

Also get used to using NSString literals; @ "example" for if / and when you decide to switch to Cocoa. Good luck with your studies.

+1
source

The compiler does not know what NSObject . If you look at reference , you will see that this is part of the Foundation framework, not CoreFoundation, so:

 #import <Foundation/Foundation.h> 

instead:

 #import <CoreFoundation/CoreFoundation.h> 
+6
source

Perhaps you really don't need curly braces on your empty interface:

 @interface Duck : NSObject @end 
0
source

Try using import instead of include. Also, make sure that the CoreFoundation framework is actually part of your project.

-1
source

All Articles