2014-02-25 62 views
1

我很困惑。我有以下代碼:嘗試獲取不爲空的對象

#import "Array.h" 

@implementation Array 
@synthesize sections; 

-initArray:(NSUInteger)s 
{ 
    if(self=[self init]) { 
     sections = [[NSMutableArray alloc] initWithCapacity:s]; 
     for (int i=0; i<=s; i++) { 

      [sections insertObject:[NSNull null] at Index:i]; 

     } 
    } 

    return self; 
} 

//這裏是一個數組填充

-(void)setObject:(NSNumber *)object:(NSUInteger)intSection 
{ 
    [sections replaceObjectAtIndex:intSection withObject:object]; 

} 

//嘗試init和填充對象

-(id)initAtAll:(NSMutableArray *)obj 
{ 
    NSMutableArray *array; 
    NSUInteger i = array.count; 

    NSObject* arrayObject = [self initArray:i]; 
    NSNumber *ggg = [NSNumber numberWithInt:777]; 
    [self setObject:ggg :0 :0];      // I got an error; 

    return self; 


} 
+(id)createObj:(NSMutableArray *)obj 
{ 
    return [[self alloc]initAtAll:(NSMutableArray*)obj]; 
} 


@end 
中的ViewController

當我打電話createObj我有一個空數據的數組。我想收到一個數據不爲空的對象。我可以如何填寫它?由於

+0

究竟什麼是'-initArray:(NSUInteger)s'認爲你缺少的返回類型。這只是一個錯字嗎? – Popeye

+0

同時在你的方法中使用'init'&'set'前綴,而他們不做他們應該做的事情是一個不好的習慣。 – Alladinian

回答

0

我相信你想,當你收到此錯誤調用這個方法:

-(void)setObject:(NSNumber *)object:(NSUInteger)intSection 

函數聲明需要兩個參數 - 對象和intSection但是當你把它叫做你逝去的3:

[self setObject:ggg :0 :0]; 

您可以將其替換到:

[self setObject:ggg :0]; 

它應該做的工作,但我強烈建議您在聲明方法時爲每個參數命名,讀起來更容易。你可以在方法聲明更改爲:

-(void)setObject:(NSNumber *)object section:(NSUInteger)intSection 
{ 
    [sections replaceObjectAtIndex:intSection withObject:object]; 

} 

,並調用它像:

[self setObject:ggg section:0]; 
+0

噢......這是我的錯誤。我是這樣寫的'[self setObject:ggg section:0];'我只是寫在這裏不太對 – user2032083

相關問題