对象未初始化
问题描述:
所以我想在Objective-C中创建一个Circle类。我知道Java和OOP,还有一点Objective-C,但我无法弄清楚为什么我的类不会初始化。这里是Circle.h:对象未初始化
#import <Foundation/Foundation.h>
#import "Image.h"
@interface Circle : NSObject
{
Image *img;
CGPoint pos;
}
-(id) init;
-(void) draw;
@end
这里是Circle.m文件:
#import "Circle.h"
@implementation Circle
-(id) init{
self = [super init];
if (self != nil) {
img = [[Image alloc] initWithImage:[UIImage imageNamed:@"circle.png"]];
pos = CGPointMake((CGFloat)200, (CGFloat)200);
}
img = [[Image alloc] initWithImage:[UIImage imageNamed:@"circle.png"]];
pos = CGPointMake((CGFloat)200, (CGFloat)200);
return self;
}
-(void) draw{
[img renderAtPoint: pos centerOfImage: YES];
}
@end
然后在我的主类我称之为:
//player is the name of the circle object (Circle *player;)
[player init];
在我的渲染方法我打电话:
[player draw];
当我运行我的应用程序时,ima ge不会画,有帮助吗?
答
你可能想说:
player = [[Circle alloc] init];
而不是仅仅[player init]
,因为后者会做什么,如果球员是零(会员默认值)。
好吧,工作!我只需要确保我这么做(来自Java背景)非常感谢! – TennisMaster 2012-07-30 19:44:14