any mistakes using @property and @synthesize

断了今生、忘了曾经 提交于 2019-12-13 07:53:48

问题


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?


回答1:


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];


来源:https://stackoverflow.com/questions/17996426/any-mistakes-using-property-and-synthesize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!