尝试获取非空的对象

时间:2014-02-25 10:09:26

标签: objective-c arrays object

我很困惑。我有以下代码:

#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];

}

//尝试初始化并填充对象

-(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
当我调用createObj时,在ViewController中的

我得到一个包含空数据的数组。我想收到一个非空数据的对象。我怎么能填写它?感谢

1 个答案:

答案 0 :(得分:0)

我相信你在收到此错误时尝试调用此方法:

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

函数声明有两个参数 - object和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];