2009-08-26 14 views
2

是我的代码:CGFloat的阵列的帮助 - 在这里iPhone开发

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 }; 
    } 
} 

所以基本上,我想根据不同的图表类型绘制不同的梯度。但是,xCode告诉我,if/else语句中的CGFloat组件[8]未被使用并忽略其值。任何想法是什么问题

回答

9

你正在每个if/else块中声明新的组件数组。你不想这样做,你只是想将值分配给已经声明的组件数组。

像这样的东西应该工作:

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)); 
    } 
} 
+0

它不是这样工作,我已经尝试过。它报告组件行错误 – Mladen 2009-08-26 11:18:08

+0

当然没有。你不能在声明之外初始化一个数组。我编辑了源代码以使用memcpy,这可能会更好。 – 2009-08-26 12:27:56

+0

现在有效。多谢 :) – Mladen 2009-08-27 07:01:26