【问题标题】:Creating an Array From a CSV File Using Objective C使用 Objective C 从 CSV 文件创建数组
【发布时间】:2012-04-18 16:47:35
【问题描述】:

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

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

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

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

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

基本上,我认为我需要这样做;

1) 将文件读入字符串.....

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

3) 循环遍历 temp 数组,每次都创建一个 tower 和 region 对象,然后将此信息附加到两个主存储数组。

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

在此先感谢大家。

克里斯。

我对此进行了编辑以显示我的代码示例。我遇到的问题是我收到警告说

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

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

#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

}

任何额外的帮助将不胜感激。感谢您迄今为止的回复。

【问题讨论】:

  • 首先你必须在你身边做一些事情,如果你在这些尝试邮政编码之间卡住了一些地方,然后我们可以帮助你,祝你的尝试好运
  • 你能举几行CSV文件的例子并解释一下信息在哪里吗?

标签: objective-c xcode


【解决方案1】:
NSString* fileContents = [NSString stringWithContentsOfURL:filename ...];
NSArray* rows = [fileContents componentsSeparatedByString:@"\n"];
for (...
    NSString* row = [rows objectAtIndex:n];
    NSArray* columns = [row componentsSeparatedByString:@","];
...

您可能需要进行一些“stringTrimmingCharactersInSet”调用来修剪空白。

【讨论】:

    【解决方案2】:

    关于您的警告:

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

    @interface MyObject : NSObject {
        NSString *myString;
    }
    @property (assign) NSString *myString;
    @end
    
    @implementation MyObject
    @synthesize myString;
      // funky code here
    @end
    

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


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

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

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-01-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-02
        • 1970-01-01
        • 2013-05-10
        相关资源
        最近更新 更多