0

我正在创建一个使用包含一组位置的 CSV 文件的游戏。

这是其中一个 CSV 文件的简短摘录。

Name,Region,Country,Type
Bath,Somerset,United Kingdom,City
Brighton and Hove,Sussex,United Kingdom,City
Canterbury,Kent,United Kingdom,City
Chichester,West Sussex,United Kingdom,City
Durham,Durham,United Kingdom,City
Clevedon,Somerset,United Kingdom,Town

我编写了一个类来读取这些数据并创建一个包含字段(名称、区域、国家、类型)的数组和另一个包含所有数据行(字段下方的任何内容)的多维数组。

在 MySql 中,有一个函数可以将特定字段中的行分组在一起,删除我在课堂上实现的所有重复项。这就是我的问题所在。

我正在使用此函数对 Region 字段进行分组,如果在上面的 CSV 数据上使用该字段,将返回一个包含 Somerset、Sussex、West Sussex、Durham、Kent 的数组。(注意只有一个萨默塞特而不是上面的两个)。它如何返回带有空格的区域,即“萨默塞特”。

我检查了 CSV 文件,并删除了我找到的所有空格,并确保它们不是任何拼写错误的区域,但这并没有解决它。当我在班级分组之前在控制台中输出所有区域时,所有区域似乎都很好。

但是我发现当这些区域被放入一个 NSSet 时,问题就出现了。CSV 数据中后面没有空格的区域,突然有一个重复的版本,最后有一个空格。(即当它应该只有一个 Somerset 时,NSSet 有一个 Somerset 和一个“Somerset”)。

CSV 没有任何问题,我的文件读取例程已成功解析 CSV,因为我正确获取了所有数据。只有当我将它放入 NSSet 时,我才会遇到奇怪的问题,因为它应该完美地删除重复项。

这是我的 CSV 类代码

标题:

  //
    //  CSV.h
    //  Guess The Distance
    //
    //  Created by James Campbell on 15/02/2012.
    //  Copyright (c) 2012 Cirencester College. All rights reserved.
    //

    #import <Foundation/Foundation.h>

    @interface CSV : NSObject {
        NSMutableArray *fields;
        NSMutableArray *rows;
    }
    @property (strong, readonly) NSMutableArray *fields;
    @property (strong, readonly) NSMutableArray *rows;
    -(CSV *)initWithCSVNamed:(NSString *)name fieldSeperator:(NSString *)fieldSeperator rowSeperator:(NSString *)rowSeperator;
    -(NSDictionary *)getRowAtIndex:(NSUInteger)index;
    -(NSArray *)getRowsForField:(NSString *)field Group:(BOOL)group;
    @end

执行:

//
//  CSV.m
//  Guess The Distance
//
//  Created by James Campbell on 15/02/2012.
//  Copyright (c) 2012 Cirencester College. All rights reserved.
//

#import "CSV.h"

@implementation CSV
@synthesize fields, rows;
-(CSV *)initWithCSVNamed:(NSString *)name fieldSeperator:(NSString *)fieldSeperator 
    rowSeperator:(NSString *)rowSeperator{
    self = [super init];
    NSLog(@"Reading CSV File...");
    NSString *filePath;
    if ([[name pathExtension] isEqualToString:@""]){
        filePath = [[NSBundle mainBundle] pathForResource:name ofType:nil];
    } else {
        filePath = [[NSBundle mainBundle] pathForResource:[[name lastPathComponent] stringByDeletingPathExtension] ofType:[name pathExtension]];
    }
    NSData *csvData = [NSData dataWithContentsOfFile:filePath]; 
    if (csvData) {  
        NSString *csvString = [[NSString alloc] initWithData:csvData encoding:NSASCIIStringEncoding];
        NSArray *csvRows = [csvString componentsSeparatedByString:rowSeperator];
        rows = [[NSMutableArray alloc] initWithCapacity:[csvRows count]-1];
        __block NSIndexSet *fieldIndexes;
        [csvRows enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            NSArray *csvFields = [(NSString *)obj componentsSeparatedByString:fieldSeperator];

            if(idx == 0){
                 fieldIndexes = [csvFields indexesOfObjectsPassingTest:
                                       ^BOOL (id el, NSUInteger i, BOOL *stop) {
                                           return (![(NSString *)el isEqualToString:@""]);
                                       }];
                fields = [[NSMutableArray alloc] initWithArray:[csvFields objectsAtIndexes:fieldIndexes]];;
            } else {
                [rows insertObject:[csvFields objectsAtIndexes:fieldIndexes] atIndex:idx-1];
            }
        }];
    }
    return self;
}
-(NSDictionary *)getRowAtIndex:(NSUInteger)index{
    NSArray *row = [rows objectAtIndex:index];
    return [[NSDictionary alloc] initWithObjects:row forKeys:fields];
}

-(NSArray *)getRowsForField:(NSString *)field Group:(BOOL)group{
    NSUInteger fieldIndex = [fields indexOfObject:field];
    id items;
    if (!group){
         NSMutableArray *itemArray = [[NSMutableArray alloc] init];
        items = itemArray;
    } else {
        NSMutableSet *itemSet = [[NSMutableSet alloc] init];
        items = itemSet;
    }
    [rows enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        NSString *row = [(NSArray *)obj objectAtIndex:fieldIndex];
        NSLog(@"|%@|", row);
        [items addObject:row];
    }];
    NSLog(@"%@", [items description]);
    if (!group){
        return [items copy];
    } else {
        return  [items allObjects];
    }
}
@end
4

0 回答 0