从逗号分隔文件加载类的最佳方法

发布于 2024-10-07 23:07:01 字数 440 浏览 2 评论 0原文

我对 Objective-C 和面向对象编程总体来说相当陌生,并且有一个理论、风格类型的问题。我想要做的是加载一个类表,其中包含逗号分隔文件中的条目。文件中的数据由许多条目组成,这些条目由一个短键后跟多个字符串值组成,所有字符串值均以逗号分隔。

有一百万种方法可以做到这一点,但我要问的是,从严格的理论角度来看,最好的方法是什么。我暂时不想使用任何类型的 XML 编码,但一旦我得到一个入口程序,最终可能会转换为该格式。

我可以使用一个函数来获取“下一条记录”,并将一个结构传入和传出函数,创建该类的新实例,从结构中加载它,然后将其添加到数组中。我首先使用 stringWithContentsOfFile 方法将文件加载到字符串中,然后使用字符串函数和一些指针遍历文件以返回结构元素,然后将其加载到类中。

这似乎是在 Objective-C 中执行此操作的合理方法,还是有更好的方法,理论上可能更合理,至少也能工作?

I am a fairly new to objective-c and to object oriented programming in general and have a theoretical, stylistic type question. What I want to do is load a table of classes with entries from a comma delimited file. The data in the file consists many entries made up of a short key followed by several string values, all delimited by commmas.

There are a million ways to do this but I'm asking what would be the way which would be best from a strictly theoretical point of view. I'd like to stay away from any sort of XML coding for the moment but will probably eventually convert to that format once I get an entry program together.

I could use a function to get the 'next record' and pass a structure in and out of the function, create a new instance of the class, load it up from the structure, then add it into an array. I'd use the stringWithContentsOfFile method to load the file into a string initially then use string functions and some pointers to march through the file to return the structure elements which I would then load into the class.

Does this seem like a reasonable way to do this in objective-c or is there a better method which is perhaps more theoretically sound which would work at least as well?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

是你 2024-10-14 23:07:01

您有一个 CSV 文件,想要阅读它吗? 有一些代码

最简单的方法是这样的:

#import "CHCSV.h"

NSString * csvFile = ...; //path to the CSV file
NSError * error = nil;
NSArray * contents = [NSArray arrayWithContentsOfCSVFile:csvFile
                                                encoding:NSUTF8StringEncoding
                                                   error:&error];
if (contents == nil) {
    NSLog (@"Error %@", error);
} else {
    for (NSArray * row in contents) {
        NSLog(@"CSV fields in this line: %@", row);
        // "row" contains all the fields (as NSStrings) that were present
        // on this line of CSV
    }
}

You have a CSV file, and you want to read it? There's some code for that.

The simplest approach would be something like:

#import "CHCSV.h"

NSString * csvFile = ...; //path to the CSV file
NSError * error = nil;
NSArray * contents = [NSArray arrayWithContentsOfCSVFile:csvFile
                                                encoding:NSUTF8StringEncoding
                                                   error:&error];
if (contents == nil) {
    NSLog (@"Error %@", error);
} else {
    for (NSArray * row in contents) {
        NSLog(@"CSV fields in this line: %@", row);
        // "row" contains all the fields (as NSStrings) that were present
        // on this line of CSV
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文