2013-05-14 81 views
-2

我试图通过解析一个.csv文件然后我运行它通过这个函数来创建一个数组。函数不能识别数组值

//Array 

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"499CSV" ofType:@"csv"]; 
NSString *csvString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil]; 

NSArray *locations = [csvString componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]]; 

NSMutableArray *secondArray = [NSMutableArray array]; 
for (NSString * location in locations) 
{ 

NSArray *components = [location componentsSeparatedByString:@","]; 

double latitude = [components[0] doubleValue]; 
double longitude = [components[1] doubleValue]; 
NSString *station = components[2]; 

NSDictionary *dict = @{@"kLatitude": @(latitude), 
         @"kLongitude": @(longitude), 
         @"kStation": station}; 

[secondArray addObject:dict]; 

} 

//Comes Out 

secondArray = (
    { 
    kLatitude = "41.656467"; 
    kLongitude = "-81.277963"; 
    kStation = 27200; 
}, 
    { 
    kLatitude = "41.657118"; 
    kLongitude = "-81.276545"; 
    kStation = 27650; 
}, 
    { 
    kLatitude = "41.658493"; 
    kLongitude = "-81.27354200000001"; 
    kStation = 28632; 
}... 


//function 

NSArray *orderedPlaces = [locationsArray sortedArrayUsingComparator:^(id a,id b) { 

NSDictionary *dictA; 
NSDictionary *dictB; 
CLLocation *locA; 
CLLocation *locB; 

dictA = (NSDictionary *)a; 
dictB = (NSDictionary *)b; 
locA = [[CLLocation alloc] initWithLatitude:[[dictA objectForKey:kLatitude] doubleValue]longitude:[[dictA objectForKey:kLongitude] doubleValue]]; 
locB = [[CLLocation alloc] 
     initWithLatitude:[[dictB objectForKey:kLatitude] doubleValue] 
     longitude:[[dictB objectForKey:kLongitude] doubleValue]]; 

问题是该函数不能识别数组值。我想这与我如何定义值有关。具体来说就是调用kLatitude和kLongitude。

有人能找出为什么像它的firstArray值我的函数不读secondArray值?我该如何解决它?预先感谢您的时间。

+0

有什么区别换行符?我没看到它。 – rdelmar 2013-05-14 02:06:24

+0

我更新了我的问题,以更好地说明问题。请查阅。 – JBeesky 2013-05-14 02:21:45

+2

你撒谎。第一输出列表中本来不会发生这些'可见#define'语句。如果他们是不可见的,你不可能构建了您的数组,你说你做的方式。请张贴真实的代码和真实的输出。 – 2013-05-14 03:36:05

回答

2

您已经定义词典键:

#define kStation @"station" 
#define kLatitude @"latitude" 
#define kLongitude @"longitude" 

尝试:

NSDictionary *dict = @{kLatitude : @(latitude), 
         kLongitude: @(longitude), 
         kStation : station}; 

你在你的第一阵列创建使用它们,而不是在第二位。

1

试试这个代码,

1)它总是更好地应对您所定义的按键,
2)获得双值之前,请确保你没有空格并在该字符串

NSCharacterSet *whiteSPNewLine = [NSCharacterSet whitespaceAndNewlineCharacterSet]; 
    for (NSString * location in locations) 
    { 

     NSArray *components = [location componentsSeparatedByString:@","]; 

     double latitude = [[components[0] stringByTrimmingCharactersInSet:whiteSPNewLine] doubleValue]; 
     double longitude = [[components[1] stringByTrimmingCharactersInSet:whiteSPNewLine] doubleValue]; 
     NSString *station = [components[2] stringByTrimmingCharactersInSet:whiteSPNewLine]; 

     NSDictionary *dict = @{kLatitude: @(latitude), 
           kLongitude: @(longitude), 
           kStation: station}; 

     [secondArray addObject:dict]; 

    }