xcode类保持不变

时间:2015-03-30 19:06:52

标签: xcode class initialization

我创建了以下类,当我尝试将类导入另一个文件,创建实例并获取或设置任何属性时,我创建了以下类,它不起作用。我已经通过调试发现,当我创建一个类的实例时,它仍然是nill。

当我看到它时,我创建了一个类似下面的init方法来为属性分配内存:

-(id)init{
    self = [super init];
    if(self){
        numberName = [[NSString alloc] init];
        numberNode = [[CCNode alloc] init];
    }

    return self
}

它仍然不起作用,并且在调试时类的实例保持为nill。 (我也尝试创建自己的getter和setter,但我认为这不是问题的一部分,因为它首先没有被初始化。)

.h文件:

#import <Foundation/Foundation.h>

@interface Number : NSObject

@property (nonatomic, assign) NSInteger numberId;
@property (nonatomic, strong) NSString *numberName;
@property (nonatomic, strong) CCNode *numberNode;

@end

.m文件:

#import "Number.h"

@implementation Number

@synthesize numberId;
@synthesize numberName;
@synthesize numberNode;

@end

1 个答案:

答案 0 :(得分:2)

您未能返回初始化对象。

-(id)init{
    self = [super init];
    if(self){
        numberName = [[NSString alloc] init];
        numberNode = [[CCNode alloc] init];
    }

    return self; // <---- This is what you're missing.
}