Question

I'm a newbie of Objective-C language and I'm trying to understand the basic concepts. I came from Java language so I have already know the principle of OOP programming. Here it is the code I wrote. It's very simple but it doesn't work properly. I have some issues using @property and @synthesize

#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;
@synthesize 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 setbrand], [myCar setyear]);
}

I don't know where it's the error. I'm pretty sure there is some mistakes in main function. is it correct call that methods in that way?

Was it helpful?

Solution

The setters must be capitalized properly.

[myCar setBrand:@"BMW Z4"];
[myCar setYear:2010];

The getters default to the property name.

NSString *carBrand = [myCar brand];
int       carYear  = [myCar year];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top