从逗号分隔文件加载类的最佳方法
我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您有一个 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: