xao
xao

Reputation: 3

XCode Throwing Errors @implementation

I'm trying to learn some of the basics of developing OS X apps with XCode and Objective-C, but I am already running into problems.

I have a project I made from a while back which worked very well for me, however, when I try to replicate the results I had last time, I run into numerous errors.

I have two files, a .c and a .h named "AppDelegate"

in AppDelegate.h:

    #import <Cocoa/Cocoa.h>
    #import <WebKit/WebView.h>

    @interface AppDelegate : NSObject {
        IBOutlet WebView *gameFrame;
    }

    @end

then, in AppDelegate.c:

    #import "AppDelegate.h"

    @implementation AppDelegate
    -(void)awakeFromNib
    {

    }
    @end

In IB, there is an NSObject named 'AppDelegate' and its class is 'AppDelegate'.

However, when I try to run this, I get 11734 errors...

When I click on the error icon at the bottom of the XCode window, it lists a bunch of code that seems to be involving NSStrings, but I cant make any sense of it...

Also, within my code, the

    @end

line in both the .c and the .h are highlighted with an error saying:

'Expected identifier or '(' before '@' token.'

I don't understand what XCode is tripping up on when it tries to compile, I don't see any logical place for a '(' to go and I don't think I left anything unidentified.

Any help would be appreciated.

Upvotes: 0

Views: 850

Answers (1)

Peter Hosey
Peter Hosey

Reputation: 96333

That's because that isn't valid C code.

You named your module file AppDelegate.c, which indicates that it contains source code written in (more or less) pure C. But it does not: You wrote a class interface and implementation in Objective-C, which is a superset of C (all C is valid Objective-C, but not all Objective-C is valid C—in particular, classes aren't).

For this, you must name the module file AppDelegate.m (or anything else, as long as it ends with .m; naming it after the class is a convention worth following). The .m suffix indicates a module (usually containing a class implementation) written in Objective-C.

So, just rename your module file from AppDelegate.c to AppDelegate.m. Make sure you do this in Xcode, not the Finder: If you do it in the Finder, Xcode will only care that there is no longer a file named AppDelegate.c; it won't notice the rename.

For your convenience in creating future classes, Xcode provides a template in the “New File” panel for creating subclasses of certain Cocoa classes; your AppDelegate should be a subclass of NSObject, and templates are also provided for NSView, NSDocument, UIView, UIViewController, and a few others. The files created by the template will already have the correct extensions.

Upvotes: 3

Related Questions