【发布时间】:2015-02-03 18:55:17
【问题描述】:
我是 Objective C 的新程序员...我的代码有一些问题,我不知道为什么会出错... 这是我的代码: 矩形.h:
#import <Foundation/Foundation.h>
@interface Rectangle : NSObject
{
int width;
int height;
}
@property(setter=setWidthOfRect: , getter=getWidth) int width;
@property(setter=setHeightOfRect: , getter=getHeight) int height;
@end
矩形.m:
#import "Rectangle.h"
@implementation Rectangle
@synthesize width, height;
@end
这是 main.m:
#import <Foundation/Foundation.h>
#import "Rectangle.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
Rectangle *rect = [[Rectangle alloc] init];
[rect setWidthOfRect:22];
[rect setHeightOfRect:28];
NSLog(@"the area is: %d", [rect getHeight] * [rect getWidth]);
}
return 0;
}
我不明白:“区域是 616 有什么帮助吗?
【问题讨论】:
-
我做到了,它给出了正确的答案。
-
我认为你没有在看 LOG 或者它可能已关闭,这意味着你在看其他东西,而不是控制台。
-
看起来没问题,除了上面代码中的一些复制粘贴,我正在编辑它。
-
它突然起作用了!!我不知道为什么以前不起作用! ,谢谢@Duaan
-
一个重要提示:Obj-C 在 getter 中不使用
get前缀。
标签: ios objective-c iphone macos