2015-03-30 88 views
0

我創建了以下類,當我嘗試將類導入另一個文件,創建實例並獲取或設置任何屬性時,它不起作用。我發現在調試時發現,當我創建一個類的實例時,它保持不變。xcode class stays nill

當我看到,我做了類似下面的init方法來分配內存的性能:

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

    return self 
} 

它仍然沒有工作,調試時的類的實例保持nill。 (我也試圖創建自己的getter和setter方法,但是我geuss這不是因爲它並沒有被擺在首位初始化的問題的一部分。)

.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 
+0

以上所有看起來大多都沒問題;我不得不在初始化例程中將下劃線('_')放在ivars前面(FYI:您不再需要@synthesize)......您是如何創建實例的? – geowar 2015-04-01 14:07:00

回答

2

您無法返回初始化對象。

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

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

對不起,忘了輸入。我已經返回自己,否則會產生一個錯誤! – Sliver2009 2015-03-30 19:24:23

+0

好的。糾正你的問題,人羣可以從那裏出發。我們只能用你給我們的東西來工作。 – 2015-03-30 19:26:07