Objective-C syntax error

Asked

Viewed 72 times

0

I’m a beginner in language Objective C and I was practicing, when I got 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;
   }

List of Errors:

Parse Issue Expected Identifier or '('
Semantic Issue Use of undeclared Identifier 'Ucas'
Semantic Issue Missing '[' at start of message send Expression
Semantic Issue Use of undeclared Identifier 'Lucas' (3x)

  • Which error?

  • The first eh: Parse Issue Expected Identifier or '(' / in the rest: Semantic Issue - Use of undeclared idenfier 'Lucas'

  • @Lucascastellani Report errors specifically in the question, so it’s easier for Sopt users to help you better. Any doubt read How to ask a good question at the aid plant

  • 1

    @Caputo Pardon, I detailed the errors in the question.

  • @Lucascastellani No need to apologize, we are a community and we are here for mutual help.

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 a class instance Pessoas, that is to say, lucas is a class object Pessoas.

In general, you usually put everything together in a single line of definition instead of a statement line followed by an assignment. That is, instead of:

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

it is more common to find:

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

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

  • Thank you so much for the explanation, now I finally understand (;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.