这是我的代码。这很简单,但我不明白错误
#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);
}
这是我得到的错误:
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]
答案 0 :(得分:1)
这在复制并粘贴到新的基于XCode Cocoa的命令行工具项目时可以正常工作。唯一的区别是我将您的代码添加到@autoreleasepool
:
#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;
}
@Martin R.上面的回答表明您使用的是 GNUStep 而不是XCode,因此您可能希望添加该标记,或者专门在GNUStep论坛或聊天室中寻求建议。
答案 1 :(得分:0)
#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);
}
答案 2 :(得分:-1)
您需要在界面中添加属性。
此
@interface Car {
@protected
NSString *brand;
int year;
}
@property(nonatomic,retain) NSString *brand;
@property int year;
@end
而不是
@interface Car: NSObject
@property(nonatomic,retain) NSString *brand;
@property int year;
@end //Car Interface
应该有效 - 但我还没试过。