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.
You should subclass
NSObject, notObject.Objectis part of the Apple/GNUStep runtimes as a legacy class, but is not used for new development.