1
votes

I am learning Objective C

I have the following trouble.

If I use the libretto NSObject my program work fine, but if I change to use the library Object.h, the program doesn't work.

The code is:

//-------------------------------------------------------------
/* Saludar.h */
//-------------------------------------------------------------
#import <objc/Object.h>

@interface  Saludar : Object
{
    char* strSaludo;
}
- (id)init;
- (void)setSaludo:(char*)sSaludo;
- (void)setSaludo:(char*)sSaludo y:(char*)sMensaje;
- (void)subSaludar;
@end

// ---------------------------------------------------------------
// Saludar.m
// ---------------------------------------------------------------
#import "Saludar.h"
#import <stdio.h>
#import <stdlib.h>
#import <string.h>

@implementation Saludar

- (id)init
{
    if (self = [super init])
    {
        strSaludo = "Programando en Objective-C";
    }
    return self;
}

- (void)setSaludo:(char*)sSaludo
{
    strSaludo = sSaludo;
}

- (void)setSaludo:(char*)sSaludo y:(char*)sMensaje
{
    // Reserva memoria para el Mensaje
    strSaludo = malloc(strlen(sSaludo)+strlen(sMensaje)+1);

    // Copia el Saludo y el Mensaje
    strcpy(strSaludo,sSaludo);
    strcat(strSaludo,sMensaje);

}

- (void)subSaludar
{
    // Desplliega el Saludo
    printf("%s\n",strSaludo);
}
@end


//--------------------------------------------------------
// Clase01.m
//--------------------------------------------------------
#import "Saludar.m"

int main()
{

    //printf("Entra al Programa\n");
    Saludar* s = [[Saludar alloc] init];


    //printf("Va a Saludar\n");
    [s subSaludar];
    [s setSaludo:"Hola Jaor"];
    [s subSaludar];
    [s setSaludo:"Hola Jaor " y:"Long Live to Programming"];
    [s subSaludar];

    [s free];
    return EXIT_SUCCESS;
}

It's like it doesn't recognize the super class 'super' or doesn't recognize the message 'init'.

The messages 'alloc' and 'free' aren't recognized.

This is all message

n Framework GNU\Saludar.m||In function '-[Saludar init]':| n Framework GNU\Saludar.m|11|warning: 'Object' may not respond to '-init' [enabled by default]| n Framework GNU\Saludar.m|11|warning: (Messages without a matching method signature [enabled by default]| n Framework GNU\Saludar.m|11|warning: will be assumed to return 'id' and accept [enabled by default]| n Framework GNU\Saludar.m|11|warning: '...' as arguments.) [enabled by default]| n Framework GNU\Clase02a.m||In function 'main':| n Framework GNU\Clase02a.m|8|warning: 'Saludar' may not respond to '+alloc' [enabled by default]| n Framework GNU\Clase02a.m|18|warning: 'Saludar' may not respond to '-free' [enabled by default]| ||=== Build finished: 0 errors, 6 warnings ===|

The program runs, but doesn't display messages, doesn't work.

2

2 Answers

3
votes

You should subclass NSObject, not Object. Object is part of the Apple/GNUStep runtimes as a legacy class, but is not used for new development.

0
votes

Using Object seems like a legitmate thing to do while learning Objective-C. You'd want to use Object.h in order to, say, start with a simpler root class. I ran into a similar problem trying to use Object on OSX.

So here's what's going on with Object, on OSX at least:

On recent versions of OSX, Apple has gutted Object.h, forcibly deprecating it. (If you take a look at /usr/include/Object.h in OSX 10.5 or later, you'll see that the old version has been ifdefed out with __OBJC2__, and the new version is in fact missing the methods in question.) The relationship between Apple and Objective-C is confusing to me, so it's hard to say whether doing this without a straightforward way to opt out of 2.0 is obnoxious or not.

I suspect that you could get around this problem by defining your own root class, or by finding a way to link the old objective-c runtime instead of the new one. Or of course you could just capitulate and use NSObject where you would use Object, and chalk it up to using Objective-C 2.0.

If you're going to go the second way, you'll need to tweak the examples you're using. Instead of including Object.h, include Foundation.h, and instead of linking using -lobjc, link using -framework Foundation (which will include libobjc), like so:

#include <Foundation/Foundation.h>

int main()
{
  NSObject* obj = [NSObject new];
  [obj release];
  return 0;
}

Built using

clang main.m -framework Foundation

Run using

./a.out