Ole*_*leB 9 csv objective-c ios
这是一个比错误相关的问题更开放的问题,所以如果你不喜欢回答这些问题,请不要火焰.
我在.csv文件中有一个巨大的(!)船只列表,用.分隔 ,
矩阵的组织方式如下:

用不同的数据重复约500次. 
现在,我希望将其读入对象,可以进一步用于填充UITableView
当前,我将数据硬编码到目标文件中,就像这样
arrayWithObjectsForTableView = [[NSMutableArray alloc] init];
if ([boatsFromOwner isEqualToString:@"Owner1"]) {
    cargoShips* ship = [[cargoShips alloc]init];
    ship.name = @"name1";
    ship.size = 1000;
    ship.owner = @"Owner1";
    [self.boatsForOwner addObject:ship];
    ship = [[cargoShips alloc]init];
    ship.name = @"Name 2";
    ship.size = 2000;
    ship.owner = @"Owner2";
Run Code Online (Sandbox Code Playgroud)
if-else的依此类推.这是一个糟糕的方法,因为1)它很无聊并需要很长时间2)如果我想更新信息需要更多时间.所以,我认为从矩阵中以编程方式读取而不是自己做它会更聪明.是的,船长显然是为了访问我的大脑.
那么,对于这个问题!
如何读取如下所示的.csv文件: 

NSMutableArray以对象的形状将
所有者的船只添加到a 中.(所以他们可以用来喂我UITableView的船.
我还想选择按照不同的东西进行排序,例如构建国家,运营商等.如何将从.csv读取的相关船只的代码编写到对象中?我不太了解编程,因此非常感谢深入的答案.
Eph*_*era 11
处理的深度将决定此任务所需的数据结构类型.这是我将使用的方法:
1:将.csv文件读入一个巨大的NSString对象:
NSString *file = [[NSString alloc] initWithContentsOfFile:yourCSVHere];
Run Code Online (Sandbox Code Playgroud)
2:获取各行:
NSArray *allLines = [file componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
Run Code Online (Sandbox Code Playgroud)
3:对于每一行,获取各个组件:
for (NSString* line in allLines) {
    NSArray *elements = [line componentsSeparatedByString:@","];
    // Elements now contains all the data from 1 csv line
    // Use data from line (see step 4)
}
Run Code Online (Sandbox Code Playgroud)
4:这取决于你.我的第一个想法是创建一个类来存储你的所有数据.例如:
@interface Record : NSObject
//...
@property (nonatomic, copy) NSString *name
@property (nonatomic, copy) NSString *owner
// ... etc
@end
Run Code Online (Sandbox Code Playgroud)
4a:然后,在步骤3中Record为每一行创建一个对象,然后将所有Record对象放入一个单独的NSArray(具有更大范围的东西!).
5:使用NSArray包含所有Record对象的对象作为您的数据源UITableView.
步骤4和5的实施由您决定.对于中等大小的.csv文件,我可能会这样做.
编辑:这是如何生成的Records.
//
NSMutableArray *someArrayWithLargerScope = [[NSMutableArray alloc] init];
//
NSString *file = [[NSString alloc] initWithContentsOfFile:yourCSVHere];
NSArray *allLines = [file componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet];
for (NSString* line in allLines) {
    NSArray *elements = [line componentsSeparatedByString@","];
    Record *rec = [[Record alloc] init];
    rec.name = [elements objectAtIndex:0];
    rec.owner = [elements objectAtIndex:1];
    // And so on for each value in the line.
    // Note your indexes (0, 1, ...) will be determined by the
    // order of the values in the .csv file.
    // ...
    // You'll need one `Record` property for each value.
    // Store the result somewhere
    [someArrayWithLargerScope addObject:rec];
}
Run Code Online (Sandbox Code Playgroud)
        |   归档时间:  |  
           
  |  
        
|   查看次数:  |  
           4859 次  |  
        
|   最近记录:  |