在分配期间iOS指定初始化程序问题

时间:2013-11-21 08:23:00

标签: ios iphone exception

我在头文件中定义了三个属性,如下所示:

@property (nonatomic,strong) NSNumber *forCount;
@property (nonatomic,strong) NSNumber *againstCount;
@property (nonatomic,strong) NSNumber *neutralCount;

在我的.m文件中,我使用以下方法合成:

@synthesize forCount = _forCount;
@synthesize againstCount = _againstCount;
@synthesize neutralCount = _neutralCount;

我指定的初始化程序定义为:

- (id)initWithCounts:(NSNumber *)forInitCount
       againstCount:(NSNumber *)againstInitCount
       neutralCount:(NSNumber *)neutralInitCount
{
    NSLog(@"entering chartView initialization");

    NSLog(@"chartView initialization. data being sent: forCount:%@, againstCount:%@, neutralCount: %@",forInitCount,againstInitCount,neutralInitCount);

    if (self = [super init]) {

        [self setForCount:forInitCount];
        [self setAgainstCount:againstInitCount];
        [self setNeutralCount:neutralInitCount];

    }

    NSLog(@"chartView initialization. forCount:%@, againstCount:%@, neutralCount: %@",self.forCount,self.againstCount,self.neutralCount);

    [self setup];
    return self;
}

我在设置视图中绘制图表。

对属性的分配似乎不起作用。这就是我的日志:

2013-11-21 03:13:20.897 testProj[4744:70b] entering chartView initialization
2013-11-21 03:13:20.897 testProj[4744:70b] chartView initialization. data being sent: forCount:10, againstCount:1, neutralCount: 5
2013-11-21 03:13:20.931 testProj[4744:70b] ChartView forCount: (null), againstCount: (null), neutralCount: (null)
2013-11-21 03:13:20.933 testProj[4744:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[0]'

知道为什么这些属性仍然保留为(null)

注释掉[self setup]方法也会导致相同的(null)分配。 - 因此,我可以排除设置方法的作用。

我正在调用这样的方法:

 NSLog(@"chartViewAtIndex about to be called. forCount:%@, againstCount:%@ and neutralCount:%@",self.forVotes[index],self.againstVotes[index],self.neutralVotes[index]);

ChartView *chartView = [[ChartView alloc]initWithCounts:self.forVotes[index] againstCount:self.againstVotes[index] neutralCount:self.neutralVotes[index]];

日志显示:

2013-11-21 09:43:46.551 testProj[5284:70b] chartViewAtIndex about to be called. forCount:10, againstCount:1 and neutralCount:5

赋值后的break语句,就在NSLog行显示赋值正确之前:

_forCount   __NSCFNumber *  (int)10 0xb0000000000000a2
_againstCount   __NSCFNumber *  (int)1  0xb000000000000012
_neutralCount   __NSCFNumber *  (int)5  0xb000000000000052

1 个答案:

答案 0 :(得分:0)

解决。

对于其他任何有初始化问题的人,请仔细检查[super init]将要执行的操作。在我的情况下,我有另一个被调用的初始化程序,因此它搞砸了。另一个初始化程序称为[自我设置],没有赋值,因此崩溃。