Objective c 错误:没有属性声明
我在头文件中声明了两个属性,并在类(名称?)文件中合成了这两个属性:Objective c 错误:没有属性声明,objective-c,Objective C,我在头文件中声明了两个属性,并在类(名称?)文件中合成了这两个属性: 宽度 高度 (哈希/磅符号)导入 .m文件: (hash/pound symbol)import "Rectangle.h" @implementation Rectangle @synthesize width, height; -(void) setWidth: (int) w setHeight: (int) h { width = w; height = h; } -(int) area {
- 宽度
- 高度 (哈希/磅符号)导入
(hash/pound symbol)import "Rectangle.h"
@implementation Rectangle
@synthesize width, height;
-(void) setWidth: (int) w setHeight: (int) h
{
width = w;
height = h;
}
-(int) area
{
return width * height;
}
-(int) perimeter
{
return (width + height) * 2;
}
@end
但是,我得到了一些错误:
请原谅我对“#”符号和代码格式有问题。我很惊讶
@property width, height;
。。。这甚至可以编译。应该是:
@property int width;
@property int height;
你几乎永远不会看到这个:
-(void) setWidth: (int) w setHeight: (int) h;
相反,@property意味着setWidth:
和setHeight:
我很惊讶
@property width, height;
。。。这甚至可以编译。应该是:
@property int width;
@property int height;
你几乎永远不会看到这个:
-(void) setWidth: (int) w setHeight: (int) h;
相反,@property意味着setWidth:
和setHeight:
尝试以下操作:
@interface Rectangle : NSObject
{
int width;
int height;
}
@property int width;
@property int height;
-(int) area;
-(int) perimeter;
-(void) setWidth: (int) w andHeight: (int) h;
@end
试试这个:
@interface Rectangle : NSObject
{
int width;
int height;
}
@property int width;
@property int height;
-(int) area;
-(int) perimeter;
-(void) setWidth: (int) w andHeight: (int) h;
@end
@属性int高度、宽度@克松谢对。这就是为什么我感到惊讶,甚至编译。。。这就是OP的代码。@属性int height,width@克松谢对。这就是为什么我感到惊讶,甚至编译。。。这是OP的密码。