2012-01-31 48 views
1

我有一種情況,我從服務器中的CSV文件中讀取值,然後解析刪除逗號的文件。此數據將被繪製,因此我必須將其轉換爲CGFloat有沒有任何可能的方法?以下是我正在使用的代碼。將NSobject轉換爲CGFPoint

-(void)connection :(NSURLConnection *) connection didReceiveData:(NSData *)data{ 

[self serverConnect]; 

response = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 

NSString *stripped1 = [response stringByReplacingOccurrencesOfString:@"\r" withString:@""]; 

NSMutableArray *rows = [NSMutableArray arrayWithArray:[stripped1 componentsSeparatedByString:@"\n"]]; 
NSMutableArray *contentArray = [NSMutableArray array]; 
NSArray *components; 




for (int i=0;i<[rows count]; i++) { 
    if(i == 0 || [[rows objectAtIndex:i] isEqualToString:@""]){ 
     continue; 
    } 
    components = [[rows objectAtIndex:i] componentsSeparatedByString:@","]; 


    id x = [components objectAtIndex:0] ; 
    id y = [components objectAtIndex:1]; 


    [contentArray addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:x,@"x",y,@"y", nil]]; 
    NSLog(@"Contents of myData: %@",contentArray); 



} 

self.scatterPlot = [[TUTSimpleScatterPlot alloc] initWithHostingView:_graphHostingView andData:contentArray]; 
[self.scatterPlot initialisePlot:0]; 


} 

內容數組的對象不是CGFloat。錯誤發生在這裏

-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index 
{ 
    if ([plot.identifier isEqual:@"mainplot"]) 
     { 
     NSValue *value = [self.graphData objectAtIndex:index]; 
      **CGPoint point = [value CGPointValue];** 

現身 - [__ NSCFDictionary CGPointValue]:無法識別的選擇發送到實例0x6b77c50'

回答

2

contentArrayNSDictionary對象的NSArray和字典條目對象NSString,不NSValue

猜測,RT和UC是x,點的y值:

NSDictionary *entry = [self.graphData objectAtIndex:index]; 
CGFloat rt = [[entry objectForKey:@"RT"] floatValue]; 
CGFloat uc = [[entry objectForKey:@"UC"] floatValue]; 
CGPoint point = CGPointMake(rt, uc); 
+0

非常感謝先生。事實上,我編輯了代碼。 RT和UC是我自己的參考。我做了以下更改: [contentArray addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:x,@「x」,y,@「y」,nil]];那麼上面的答案仍然適用於x和y替換rt和uc? – 2012-01-31 19:13:34

+1

當然,只要字典鍵匹配。 – zaph 2012-01-31 19:29:24