将.csv读入NSObjects,然后按不同的标准排序 [英] Reading a .csv into NSObjects and then sorting them by different criterias

查看:141
本文介绍了将.csv读入NSObjects,然后按不同的标准排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个开放的问题,而不是与错误相关的问题,所以如果你不喜欢回答这些问题,请不要火焰。

This is more a open question than an error-related question, so if you don't like to answer these kinds of questions, please don't flame.

我有一个.csv文件中的一个巨大的(!)列表,由

I have a huge (!) list of ships in a .csv file, separated by ,

这样组织:


使用不同的数据重复约500倍。

The matrix is organised like this:
repeated with different data about 500 times.

现在,我想要将它读入对象,可以进一步用来填充 UITableView
目前,我硬编码数据到对象文件,像这样

Now, I want this to be read into objects, which can be used further to populate a UITableView Currently, I hard-code data into the object files, like this

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";

依此类推,使用if-else。这是一个坏的方法,如
1)它无聊,需要很长时间
2)如果我想更新信息,需要更多的时间。
所以,我认为从矩阵中以编程方式读取会更聪明,而不是自己做。是的,上尉明明来找我的大脑。

And so on and on with if-else's. This is a bad method, as 1) Its boring and takes a long time 2) It takes even more time if I want to update the information. So, I figured it would be smarter to read programmatically from the matrix instead of doing it myself. Yeah, captain obvious came for a visit to my brain.

所以,问题!
如何阅读。 csv文件,如下所示:

添加的船,例如,所有者,以对象的形状到 NSMutableArray 。 (所以他们可以用来向船只提供 UITableView

So, to the question! How can I read the .csv file that looks like this: add the ships of, say, owner, to a NSMutableArray, in the shape of objects. (So they can be used to feed my UITableView with ships.

我也想选择排序由不同的东西,如国家的建设,操作员等我如何使相关的船舶从.CSV读取的代码进入对象?
我不知道很多编程,所以深入的答案将非常

I would also like to have the option to sort by different stuff, like Country of build, Operator etc. How can I make code that feeds relevant ships read from the .csv into objects? I don't know much programming, so in-depth answers would be very appreciated.

推荐答案

您的处理深度将决定此任务需要什么样的数据结构,这是方法I将使用:

The depth of your processing will determine what sort of data structure is required for this task. This is the method I would use:

1: .csv 文件读入一个巨人 NSString object:

1: Read the .csv file into one giant NSString object:

NSString *file = [[NSString alloc] initWithContentsOfFile:yourCSVHere];

2:取得各行:

NSArray *allLines = [file componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];

3:对于每一行,获取各个组件:

3: For each line, get the individual components:

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)
}

4:这取决于你。我的第一个想法是创建一个类来存储所有的数据。例如:

4: This is where it's up to you. My first thought would be to create a class to store all your data. For example:

@interface Record : NSObject
//...
@property (nonatomic, copy) NSString *name
@property (nonatomic, copy) NSString *owner
// ... etc
@end

4a:然后,回到第3步,为每一行创建一个 Record 然后将所有Record对象放入一个单独的 NSArray (有更大范围的东西!)。

4a: Then, back in step 3 create a Record object for each line and then put all the Record objects into a separate NSArray (something with larger scope!).

5 :使用包含您的所有记录对象作为<$ c $的数据源的 NSArray c> UITableView 。

5: Use your NSArray that contains all your Record objects as the data source for your UITableView.

步骤4和5的实现取决于您。这可能是我如何做一个中型 .csv 文件。

The implementation of Steps 4 and 5 are up to you. That's probably how I would do it though for a medium sized .csv file.

编辑: / strong>生成记录的方法如下。

//
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];
}

这篇关于将.csv读入NSObjects,然后按不同的标准排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆