2013-05-14 42 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]; 

    }