xcode 使用 Objective C 从 CSV 文件创建数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10214084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:00:48  来源:igfitidea点击:

Creating an Array From a CSV File Using Objective C

objective-cxcode

提问by user1341967

I'm new to coding so please excuse me if this seems like a simple question.

我是编码新手,所以如果这似乎是一个简单的问题,请原谅。

I'm trying to plot coordinates on a map.

我正在尝试在地图上绘制坐标。

I want to read a CSV file and pass the information to two separate arrays.

我想读取一个 CSV 文件并将信息传递给两个单独的数组。

The first array will be NSArray *towerInfo (containing latitude, longitude and tower title)

第一个数组将是 NSArray *towerInfo(包含纬度、经度和塔标题)

the second, NSArray *region (containing tower title and region) with the same count index as the first array.

第二个 NSArray *region(包含塔标题和区域)与第一个数组具有相同的计数索引。

Essentially, I believe I need to;

基本上,我相信我需要;

1) read the file to a string.....

1)读取文件到一个字符串.....

2) divide the string into a temporary array separating at every /n/r......

2) 将字符串分成一个临时数组,在每个 /n/r 处分开......

3) loop through the temp array and create a tower and region object each time before appending this information to the two main storage arrays.

3)循环遍历临时数组并每次在将此信息附加到两个主要存储数组之前创建一个塔和区域对象。

Is this the right process and if so is there anyone out there who can post some sample code as I'm really struggling to get this right.

这是正确的过程吗?如果是,是否有人可以发布一些示例代码,因为我真的很难做到这一点。

Thanks to all in advance.

提前感谢大家。

Chris.

克里斯。

I have edited this to show an example of my code. I am having the problem that I'm receiving warnings saying

我已经编辑了它以显示我的代码示例。我遇到的问题是我收到警告说

1) "the local declaration of 'dataStr' hides instance variable. 2) "the local declaration of 'array' hides instance variable.

1)“'dataStr'的局部声明隐藏了实例变量。2)“'array'的局部声明隐藏了实例变量。

I think I understand what these mean but I don't know how to get around it. The program compiles and runs but the log tells me that the "array is null."

我想我明白这些是什么意思,但我不知道如何解决它。程序编译并运行,但日志告诉我“数组为空”。

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize dataStr;
@synthesize array;

-(IBAction)convert {
//calls the following program when the onscreen 'convert' button is pressed.

    NSString *dataStr = [NSString stringWithContentsOfFile:@"Towers.csv" encoding:NSUTF8StringEncoding error:nil];
    //specifies the csv file to read - stored in project root directory - and encodes specifies that the format of the file is NSUTF8. Choses not to return an error message if the reading fails

    NSArray *array = [dataStr componentsSeparatedByString: @","];
    //splits the string into an array by identifying data separators.

    NSLog(@"array: %@", array);
    //prints the array to screen

}

Any additional help would be much appreciated. Thanks for the responses so far.

任何额外的帮助将不胜感激。感谢您到目前为止的答复。

回答by Hot Licks

NSString* fileContents = [NSString stringWithContentsOfURL:filename ...];
NSArray* rows = [fileContents componentsSeparatedByString:@"\n"];
for (...
    NSString* row = [rows objectAtIndex:n];
    NSArray* columns = [row componentsSeparatedByString:@","];
...

You'll probably want to throw in a few "stringTrimmingCharactersInSet" calls to trim whitespace.

您可能想要加入一些“stringTrimmingCharactersInSet”调用来修剪空白。

回答by Fnord23

Concerning your warnings:

关于您的警告:

Your code would produce an error (not a warning), since you need to declare your properties in the interface file before you synthesize them in the implementation. You probably remember that @synthesizegenerates accessor methods for your properties. Also, before using the @synthesizedirective, you need to use the @propertydirective, also in the interface. Here's an example:

您的代码会产生错误(不是警告),因为您需要在接口文件中声明您的属性,然后才能在实现中合成它们。您可能还记得@synthesize为您的属性生成访问器方法。此外,在使用@synthesize指令之前,您需要使用@property指令,也在接口中。下面是一个例子:

@interface MyObject : NSObject {
    NSString *myString;
}
@property (assign) NSString *myString;
@end

@implementation MyObject
@synthesize myString;
  // funky code here
@end

Note that the property declaration is followed by a type (assign in this case, which is the default). There's an excellent explanation about this in Stephen G. Kochans's book: Programming in Objective-C 2.0

请注意,属性声明后跟一个类型(在这种情况下是赋值,这是默认值)。在 Stephen G. Kochans 的书中对此有一个很好的解释:Objective-C 2.0 中的编程



But assuming for argument's sake, that you omitted the correct @interfacefile here. If you first declare a property in the @interface, and then declare another property in your method, using the same variable name, the method variable will take precedence over the instance variable.

但是假设为了论证,您在@interface这里省略了正确的文件。如果您首先在 中声明一个属性@interface,然后在您的方法中声明另一个属性,使用相同的变量名,则方法变量将优先于实例变量。

In your code, it would suffice to omit the declaring of the variable name, like so:

在您的代码中,省略变量名称的声明就足够了,如下所示:

dataStr = [NSString stringWithContentsOfFile:@"Towers.csv" encoding:NSUTF8StringEncoding error:nil];    
array = [dataStr componentsSeparatedByString: @","];

回答by Matt J

I'm assuming that the core of your question is "how to parse a CSV file", not "what to do with the data once it's parsed". If that's the case, then check out the CHCSVParserlibrary. I have used it in projects before and find it to be very reliable. It can parse any arbitrary string or filepath into an NSArray of rows/columns for you. After that, whatever you do with the data is up to you.

我假设您问题的核心是“如何解析 CSV 文件”,而不是“解析数据后如何处理”。如果是这种情况,请查看CHCSVParser库。我之前在项目中使用过它,发现它非常可靠。它可以为您将任意字符串或文件路径解析为行/列的 NSArray。之后,您对数据的处理取决于您。