tags:

views:

185

answers:

1

Hi Guys, here is my code:

CGFloat components[8];//[8];// = {  0.6, 0.6, 0.6, 0.5, 0.4, 0.4, 0.4, 0.5 };
if ([appDelegate.graphType isEqualToString:@"response"])
{
 CGFloat components[8] = {  0.2, 0.2, 0.2, 0.5, 0.5, 0.5, 0.5, 0.5 };
}
else 
{
 if ([appDelegate.graphType isEqualToString:@"uptime"])
 {
  CGFloat components[8] = {  0.694, 0.855, 0.961, 0.5, 0.188, 0.588, 0.906, 0.5 };
 }
 else
 {
  CGFloat components[8] = {  0.694, 0.855, 0.961, 0.5, 0.188, 0.588, 0.906, 0.5 };
 }
}

So basically, I want to draw different gradients based on different graph types. However, xCode shows me that CGFloat components[8] from if/else statements is unused and ignores its values. Any ideas what is the problem

+3  A: 

You are declaring new components arrays in each if/else block. You don't want to do this, you just want to assign the values to the already declared components array.

Something like this should work:

CGFloat components[8];

const CGFloat components1[8] = {  0.2, 0.2, 0.2, 0.5, 0.5, 0.5, 0.5, 0.5 };
const CGGloat components2[8] = {  0.694, 0.855, 0.961, 0.5, 0.188, 0.588, 0.906, 0.5 };
const CGFloat components3[8] = {  0.694, 0.855, 0.961, 0.5, 0.188, 0.588, 0.906, 0.5 };

if ([appDelegate.graphType isEqualToString:@"response"])
{
    memcpy(components,components1, 8*sizeof(CGFloat));
}
else 
{
    if ([appDelegate.graphType isEqualToString:@"uptime"])
    {
             memcpy(components,components2, 8*sizeof(CGFloat));
    }
    else
    {
             memcpy(components,components3, 8*sizeof(CGFloat));
    }
}
Tom
It isn't working like this, I have tried it before. It reports error for components lines
Mladen
No of course it wasn't. You can't initialize an array outside of it's declaration. I've edited the source to use memcpy, which might work better.
Tom
It works now. Thx a lot :)
Mladen