Iphone NSSet无缘无故获得NSStrings

Iphone NSSet无缘无故获得NSStrings,iphone,objective-c,nsstring,nsset,Iphone,Objective C,Nsstring,Nsset,我正在创建一个使用包含一组位置的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 Kin

我正在创建一个使用包含一组位置的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的数组。(请注意,只有一个Somerset,而不是上面的两个)。它返回的区域在其末尾有空格,即“Somerset”

我检查了CSV文件,我已经删除了我找到的所有空间,并确保它们没有任何错拼区域,但这并没有修复它。当我在我的类分组之前输出控制台中的所有区域时,所有区域看起来都很好

然而,我发现,当将这些区域放入NSSet时,问题就会出现。CSV数据中后面没有空格的区域,突然之间会有一个重复版本的自身,其末尾有一个空格。(即NSSet有一个Somerset和一个“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

这个代码对我来说没问题。返回的字符串数组中没有额外的空格。不用担心,我发现了问题。最新的XCode没有更新它在模拟器上安装的CSV文件(即使是干净的版本)。这不是我第一次遇到新Xcode的问题。
//
//  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