NSMutableArray gives null when using addObject
I'm trying to add values to the yArray. However in the NSLog, it always shows up null. I am synthesizing the NSMutableArray (yArray) and get no errors or warnings on compiling. I believe one reason might be that i am n开发者_开发问答ot initializing the array. I read somewhere that @synthesize does not initialize. However, i'm a beginner and i'm not sure how to properly initialize the array. I've researched it on google but couldn't figure it out. Can someone tell me how or at least point me in the right direction. I posted a part of my code below. Thank you in advance.
for (int i = 0; i < n; ++i)
{
xx = xx + [[self.yArr1 objectAtIndex:i] doubleValue];
}
for (int i = 0; i < n; ++i)
{
if(i==0)
[self.yArray addObject:[NSNumber numberWithDouble:(0.0)]];
else
{
bb = fabs([[self.yArr1 objectAtIndex:i] doubleValue]- [[self.yArr1 objectAtIndex:(i-1)] doubleValue]);
[self.yArray addObject:[NSNumber numberWithDouble:(bb)]]; // This is the problem.
yy = yy + [[self.yArray objectAtIndex:i] doubleValue];
}
NSLog(@"prediction %f, %f, %@, %f", bb, yy, [self.yArray objectAtIndex:i],[[self.yArr1 objectAtIndex:i] doubleValue]);
}
NSLog(@"prediction %f, %f", bb, yy);
To properly initialize the array use
NSMutableArray* yArray = [[NSMutableArray alloc] init];
Initialize it before using it (maybe in init
method or something like that), then retry. Don't forget to manually release it when you have done.
Just initialize it first in a location that will be hit prior to your addObject
code. e.g.
yArray = [[NSMutableArray alloc] init];
精彩评论