Objective-C syntax error

0

I am a beginner in the language Objective C and I was practicing, when I received some errors, follow the code:

@interface Pessoas: NSObject
{
int idade;
int peso;
}

-(void) imprimir;
-(void) setIdade: (int) i;
-(void) setPeso: (int) p;

@end

@implementation Pessoas

-(void) imprimir{
    NSLog(@"Meu nome é Lucas, eu tenho %i anos e %i quilos", idade, peso);
}

-(void) setIdade:(int) i{
    idade = i;
}

-(void) setPeso:(int) p{
    peso = p;
}

@end

int main(int argc, const char * argv[]){
    @autoreleasepool {
        Pessoas = lucas;
        lucas = [[Pessoas alloc] init];
        [lucas setIdade:19];
        [lucas setPeso: 66];
        [lucas imprimir];
    }
    return 0;
   }

Error List:

  

Parse Issue Expected identifier or '('
  Semantic Issue Use of undeclared identifier 'lucas'
  Semantic Issue Missing '[' at start of message send expression
  Semantic Issue Use of undeclared identifier 'lucas' (3x)

    
asked by anonymous 30.04.2014 / 20:22

1 answer

2

The problem is in

Pessoas = lucas;
lucas = [[Pessoas alloc] init];

To declare an object, you must use the same syntax used in C to declare pointers:

Pessoas *lucas;

The above line declares a variable called lucas which is a pointer to an instance of class Pessoas , that is, lucas is an object of class Pessoas .

In general, you typically merge everything into a single definition line instead of a statement line followed by an assignment. This is instead of:

Pessoas *lucas;
lucas = [[Pessoas alloc] init];

It's more common to find:

Pessoas *lucas = [[Pessoas alloc] init];

Note: Class names are usually in the singular. Think of the one-example-relationship of: lucas is an example of Pessoa (singular).

    
30.04.2014 / 20:38