iOS 应用程序中的核心数据和文本文件

发布于 2024-11-07 03:04:13 字数 398 浏览 0 评论 0原文

我正在创建一个简单的 iOS 应用程序,其中包含一些 UITableViewController。视图控制器中显示的信息将来自一个文本文件(我将其包含在项目的资源中)。文本文件的内容将来自电子表格。

由于这是我第一次使用核心数据,因此我有几个问题:

  • 文本文件最常见的格式是什么? CSV、XML 还是其他格式?
  • 导入数据最简单的方法是什么?

一些注意事项:

  • 数据是静态的。理想情况下,应用程序只会将数据加载到“核心数据”中一次(应用程序第一次运行)。
  • 该应用程序的每次额外运行只会从某些核心数据源(我还不完全熟悉)中提取数据,而不是从文本文件中重新加载数据。

I'm creating a simple iOS application consisting of a few UITableViewControllers. The information displayed in the view controllers will come from a text file (that I'll include in the project's Resources). The text file's contents will come from a spreadsheet.

Since this is my first time working with Core Data I have a few questions:

  • What format is most common for the text file? CSV, XML or something else?
  • What's the easiest way to import the data?

A few notes:

  • The data is static. Ideally the app will load the data into "Core Data" just once (1st time the app is run).
  • Each additional run of the app will just pull data from some Core Data source (that I'm not completely familiar w/ yet) instead of re-loading it from the textfile.

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

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

发布评论

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

评论(2

农村范ル 2024-11-14 03:04:13

如果数据以关系方式构建,那么 XML 或 JSON 可以轻松保留该结构,然后轻松解析并保存在核心数据存储中。您需要使用 XML 或 JSON 解析器,它将您的数据转换为字典数组(如果您的数据结构需要,则可以将其转换为多个级别)。您只需迭代数组并深入字典(以及子数组和子字典,如果适用),然后随时将对象添加到存储中。

如果它是平面数据,一个简单的单个表将成为核心数据中的单个实体,那么制表符分隔或 CSV 文本文件就可以了(如果数据中没有任何制表符,则制表符分隔更容易解析本身)。然后,您可以抓取各个行,将行分解为数据位数组(这就是制表符分隔变得超级简单的地方),为每行创建一个新对象,将其属性设置为数组元素,然后保存上下文。

XML/JSON 版本比这里值得写的更复杂 - 搜索 SO,你会发现很多例子 - 但这里是制表符分隔的版本(这假设你没有一个巨大的数据球可以不能合理地保存在内存中):

// Standard Core Data setup here, grabbing the managedObjectContext, 
//     which is what I'll call it
// Then parse your text
NSString *path = [[NSBundle mainBundle] pathForResource:@"YourTextFileName" ofType:@"txt"];
NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];
NSArray *rows = [content componentsSeparatedByString:@"\n"];
// Now that we have rows we can start creating objects
YourManagedObject *yourManagedObject = nil;
for (NSString *row in rows) {
  NSArray *elements = [row componentsSeparatedByString:@"\t"];
  YourManagedObject *yourManagedObject = (YourManagedObject *)[NSEntityDescription insertNewObjectForEntityForName:@"YourManagedObject" inManagedObjectContext:managedObjectContext;
  [YourManagedObject setName:[elements objectAtIndex:0]];
  [YourManagedObject setCountry:[elements objectAtIndex:1]];
  // Etc. You may need an NSNumberFormatter and/or an NSDateFormatter to turn
  //   your strings into dates and numbers, depending on your data types
  [managedObjectContext save];
}

噗,全部完成。

If the data is structured in a relational way then XML or JSON allows that structure to be easily preserved and then easily parsed and saved in your Core Data store. You'll need to use an XML or JSON parser, which will turn your data into an array of dictionaries (or multiple levels thereof if your data structure requires it). You'll simply iterate through the array and dig into the dictionaries (and sub-arrays and sub-dictionaries, if appropriate) and add objects to your store as you go.

If it's flat data, a simple single table that will become a single entity in Core Data, then tab-delimited or CSV text files are fine (and tab-delimited is even easier to parse if there wouldn't be any tabs within the data itself). You can then grab individual rows, break the rows down into an array of data bits (this is where tab delimiting makes is super-simple), create a new object for each row, set its properties to the array elements, and save the context.

The XML/JSON version is more complex than is worth writing out here -- search SO and you'll find lots of examples -- but here's the tab-delimited version (this assumes you don't have a gigantic ball of data that can't reasonably be held in memory):

// Standard Core Data setup here, grabbing the managedObjectContext, 
//     which is what I'll call it
// Then parse your text
NSString *path = [[NSBundle mainBundle] pathForResource:@"YourTextFileName" ofType:@"txt"];
NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];
NSArray *rows = [content componentsSeparatedByString:@"\n"];
// Now that we have rows we can start creating objects
YourManagedObject *yourManagedObject = nil;
for (NSString *row in rows) {
  NSArray *elements = [row componentsSeparatedByString:@"\t"];
  YourManagedObject *yourManagedObject = (YourManagedObject *)[NSEntityDescription insertNewObjectForEntityForName:@"YourManagedObject" inManagedObjectContext:managedObjectContext;
  [YourManagedObject setName:[elements objectAtIndex:0]];
  [YourManagedObject setCountry:[elements objectAtIndex:1]];
  // Etc. You may need an NSNumberFormatter and/or an NSDateFormatter to turn
  //   your strings into dates and numbers, depending on your data types
  [managedObjectContext save];
}

Poof, all done.

泪眸﹌ 2024-11-14 03:04:13

如果数据没有改变,为什么还要在应用程序中包含文本文件呢?相反,您可以在 Mac 上创建 Core Data 文件,并将其作为资源包含在应用程序中。我认为这是很多数据,需要一段时间才能解析,因此当您可以进行一次解析并分发结果时,让您的用户等待这种情况发生是没有意义的。

为此,请从应用程序中获取数据模型和解析代码,并使用它们构建一个小型命令行应用程序,该应用程序仅读取文本文件,写入核心数据文件,然后退出。

If the data doesn't change, why bother including the text file in the app? Instead, create Core Data file on your Mac and include that as a resource in the app. I presume it's a lot of data that'll take a while to parse, so there's no sense in making your users each wait for that to happen when you could do the parsing once and distribute the result.

To make that happen, take the data model and the parsing code from your app and use them to build a small command-line app that just reads the text file, writes the Core Data file, and exits.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文