编译objective-c代码时出错
这是我的密码。这很简单,但我不理解错误编译objective-c代码时出错,objective-c,synthesize,Objective C,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, year; @end //Car
#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);
}
您需要在接口中添加属性 这个 而不是这个
@interface Car: NSObject
@property(nonatomic,retain) NSString *brand;
@property int year;
@end //Car Interface
应该可以工作-但我还没有尝试过。当复制并粘贴到新的基于XCode Cocoa的命令行工具项目中时,这很好用。唯一的区别是我将您的代码添加到了
@autoreleasepool
:
Main.m
#导入
#进口“Car.h”
int main(int argc,const char*argv[]
{
@自动释放池{
int-y;
//新奥格托酒店
Car*myCar=[[Car alloc]init];
//Setto i参数
[myCar setBrand:@“BMW Z4”];
NSLog(@“Inserisci数据模型:”);
scanf(“%i”和“&y);/E'buona norma lasciare uno spazio
[我的汽车设定年份:y];
//Stampo a视频i dati
NSLog(@“Marca:%@Anno:%i”,[myCar品牌],[myCar年份];
}
返回0;
}
上面@Martin R.的回答表明您使用的是GNUStep而不是XCode,因此您可能希望添加该标记,或者专门在GNUStep论坛或聊天室上寻求建议。#import
#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);
}
@接口车:NSObject
{
@保护
NSString*品牌;
国际年;
}
@财产(非原子,保留)NSString*品牌;
@每年的财产;
@结束//车辆接口
#进口“Car.h”;
@实施车
@综合品牌,年;
@完//Car实施
int main(int argc,const char*argv[]
{
int-y;
//新奥格托酒店
Car*myCar=[[Car alloc]init];
//Setto i参数
[myCar setBrand:@“BMW Z4”];
NSLog(@“Inserisci数据模型:”);
scanf(“%i”和“&y);/E'buona norma lasciare uno spazio
[我的汽车设定年份:y];
//Stampo a视频i dati
NSLog(@“Marca:%@Anno:%i”,[myCar品牌],[myCar年份];
返回(0);
}
这与您上一个问题中的代码几乎相同。如果答案不适合你,你为什么要接受它也许您应该再次提到您正在使用GNUstep。是的,它是相同的代码,但我解决了前面的问题,事实上,我不再有那个错误。现在问题不同了,这与GNUstep无关。我在GNUstep中使用了clang,我可以很好地编译您的代码。首先了解IVAR和属性之间的区别。是的,现在它可以工作了……但原因还不清楚。我会去研究根据你说的,用XCode编程和用GNUstep编程完全不同,所以这是真正的reason@Mazzy-您是指@autoreleasepool吗?这只是Objective-C的一个自动内存管理功能(就我有限的知识而言)——但是,是的,XCode肯定是它自己的IDE。这与GNUstep无关,与编译器有关。GCC是一个问题,或者可能是一个太旧版本的clang。不要在Objective-C中使用GCC。如果你认识任何新来者,告诉他们应该(必须)使用clang,而不是GCC。如果GCC不会伤害任何人,请卸载它。
@interface Car: NSObject
@property(nonatomic,retain) NSString *brand;
@property int year;
@end //Car Interface
#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;
}
#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);
}