Question

Here it my code. It is very simple but I don't understand the error

#import <Foundation/Foundation.h>

@interface Car: NSObject

@property(nonatomic,retain) NSString *brand;
@property int year;

@end //Car Interface

#import "Car.h"

@implementation Car

@synthesize brand, year;

@end //Car Implementation

#import "Car.h"

int main (int argc, const char * argv[])
{
    int y;

    //Creo un nuovo oggetto
    Car *myCar = [[Car alloc] init];

    //Setto i parametri
    [myCar setBrand: @"BMW Z4"];

    NSLog (@"Inserisci data modello: ");
    scanf (" %i", &y); //E' buona norma lasciare uno spazio
    [myCar setYear: y];

    //Stampo a video i dati
    NSLog(@"Marca: %@ Anno: %i", [myCar brand], [myCar year]);

    return (0);
}

Here the errors I got:

car.m:5:1: error: ivar 'brand' used by '@synthesize' declaration must be an existing iva
car.m:5:1: error: ivar 'year' used by '@synthesize' declaration must be an existing ivar
car.m:7:1: warning: incomplete implementation of class 'Car' [enabled by default]
car.m:7:1: warning: method definition for '-setBrand:' not found [enabled by default]
car.m:7:1: warning: method definition for '-brand' not found [enabled by default]
car.m:7:1: warning: method definition for '-setYear:' not found [enabled by default]
car.m:7:1: warning: method definition for '-year' not found [enabled by default]
Was it helpful?

Solution 3

you need to add the properties within the interface.

this

@interface Car {
    @protected
        NSString *brand;
        int year;
}


@property(nonatomic,retain) NSString *brand;
@property int year;

@end

instead of this

@interface Car: NSObject

@property(nonatomic,retain) NSString *brand;
@property int year;

@end //Car Interface

should work - but i haven't tried.

OTHER TIPS

This works fine when copied and pasted in to a new XCode Cocoa-based Command Line Tool project. The only difference is I added your code in to the @autoreleasepool:

Main.m

#import <Foundation/Foundation.h>
#import "Car.h"

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        int y;
        
        //Creo un nuovo oggetto
        Car *myCar = [[Car alloc] init];
        
        //Setto i parametri
        [myCar setBrand: @"BMW Z4"];
        
        NSLog (@"Inserisci data modello: ");
        scanf (" %i", &y); //E' buona norma lasciare uno spazio
        [myCar setYear: y];
        
        //Stampo a video i dati
        NSLog(@"Marca: %@ Anno: %i", [myCar brand], [myCar year]);
    }
    return 0;
}

The answer by @Martin R. above indicates you're using GNUStep instead of XCode, so you may wish to add that tag, or seek advice specifically on GNUStep forums or chat rooms.

    #import <Foundation/Foundation.h>

    @interface Car: NSObject
    {
    @protected 
            NSString *brand;
            int year;

    }

    @property(nonatomic,retain) NSString *brand;
    @property int year;

    @end //Car Interface

#import "Car.h";


    @implementation Car

    @synthesize brand, year;

    @end //Car Implementation


    int main (int argc, const char * argv[])
    {
        int y;

        //Creo un nuovo oggetto
        Car *myCar = [[Car alloc] init];

        //Setto i parametri
        [myCar setBrand: @"BMW Z4"];

        NSLog (@"Inserisci data modello: ");
        scanf (" %i", &y); //E' buona norma lasciare uno spazio
        [myCar setYear: y];

        //Stampo a video i dati
        NSLog(@"Marca: %@ Anno: %i", [myCar brand], [myCar year]);

        return (0);
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top