user437038
user437038

Reputation:

What exactly is wrong with this simplistic Objective-C code?

I am currently moving from C to Objective-C and, to me, this code seems to be all find a dandy but Xcode thinks otherwise. I got this code sample from the internet and have been relentlessly trying to correct it and I've come to a deadend:

#include <objc/Object.h>

@interface Greeter:Object
{
    /* This is left empty on purpose:
     ** Normally instance variables would be declared here,
     ** but these are not used in our example.
     */
}

- (void)greet;

@end

#include <stdio.h>

@implementation Greeter

- (void)greet
{
    printf("Hello, World!\n");
}

@end

#include <stdlib.h>

int main(void)
{

    id myGreeter;
    myGreeter = [Greeter new];

    [myGreeter greet];

    [myGreeter release];
    return 0;
}

The error seems to be on the myGreeter = [Greeter new]; line and the Xcode isolates the problem as something about Thread 1. Do I need to alloc/init anything?

Below is the console log:

[Switching to process 1833 thread 0x0]
2011-04-18 21:52:10.323 PROJ[1833:903] *** NSInvocation: warning: object 0x100001160 of class 'Greeter' does not implement methodSignatureForSelector: -- trouble ahead
2011-04-18 21:52:10.326 PROJ[1833:903] *** NSInvocation: warning: object 0x100001160 of class 'Greeter' does not implement doesNotRecognizeSelector: -- abort
sharedlibrary apply-load-rules all
Current language:  auto; currently objective-c
(gdb) 

Upvotes: 0

Views: 477

Answers (6)

epatel
epatel

Reputation: 46051

Your example uses the GNU runtime and thus is a bit deprecated. The compiler defaults to the NeXT runtime but can be set to use the GNU runtime with the compile option -fgnu-runtime

You should look into grabbing a good book about Objective-C like "Programming in Objective-C" by Stephen Kochan

http://www.amazon.com/Programming-Objective-C-Stephen-Kochan/dp/0672325861

Upvotes: 0

mipadi
mipadi

Reputation: 410542

Your class, Greeter, inherits from the Objective-C Object class. In Cocoa, the root class is (generally) NSObject, and you should inherit from that. This may fix your problem.

Upvotes: 3

Nektarios
Nektarios

Reputation: 10371

Actually using new is, sort of, shorthand for the alloc/init, as you can read about here

HOWEVER, you're using objective-c outside of Cocoa it appears, because you're inheriting from Object and not NSObject and so on. So I think you should explicitly use myGreeter = [[myGreeter alloc] init];

Also since you say Xcode, you should be using Cocoa. Try:

#import <Cocoa/Cocoa.h>

And then also switch Object to NSObject

Upvotes: 0

Jonah
Jonah

Reputation: 17958

Greeter:Object should be Greeter:NSObject, "Object" is not an objective-c class.

Upvotes: 2

DJ Burb
DJ Burb

Reputation: 2364

Maybe I am wrong, but I always thought you allocated in Objective - C like this

id myGreeter;
myGreeter= [[myGreeter alloc] init];

Upvotes: 0

Steven Kramer
Steven Kramer

Reputation: 8513

Is this not just the Xcode debugger halting on the default breakpoint in "main" ? Simply click continue (or similar in the Run menu) and you should be golden.

Upvotes: 0

Related Questions