2013-01-23 133 views
-3

我试图将一个字符串解析为一个数组,在索引1处获取数组的一部分并将该字符串转换为一个浮点数。我在这里有我的代码,但我得到的错误“Intializing‘浮动’类型不兼容‘的NSString * _strong’的表达,救命啊!将字符串转换为浮点数

NSString *csv = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://download.finance.yahoo.com/d/quotes.csv?s=AAPL&f=sl1d1t1c1ohgv&e=.csv"]]; 

NSArray *array = [csv componentsSeparatedByString: @","]; 

NSString *price = [array objectAtIndex:1]; 

[price floatValue]; 

float currentPrice = price; 
+0

根据文档[floatValue](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/floatValue)返回与传递的字符串对象相对应的float值。它不会将字符串转换为浮点数。你所尝试的是通过假设'[price floatValue]''将参数'价格'转换为一个浮点数,因此你可以分配给'currentPrice',这不是它的工作方式。 – iDev

回答

3

尝试

float currentPrice = [price floatValue]; 
0

试试这个

float currentPrice = [price floatValue]; 
0
your_float = [your_string floatValue]; 

试试这个:

NSLog(@"float value is: %f", your_float);