【问题标题】:Add String Attribute to CoreDataBooks将字符串属性添加到 CoreDataBooks
【发布时间】:2011-07-08 22:57:49
【问题描述】:

我一直在搞乱 CoreDataBooks 并试图在 sqlite 文件中添加除了当前三个之外的另一个字段。我在 .xcdatamodel 中添加了属性字符串,并在 book.h、book.m 和本地化文件中声明了它(所有这些都没有更改 sqlite 文件)。但是,这些更改永远不会在 sqlite 中添加字段,并且应用程序永远不会加载。每次我在模拟器中删除应用程序并执行构建-> 清理时,但无济于事。我也尝试更改 sqlite 文件以匹配 .xcdatamodel 但应用仍然无法加载。

这是 CoreDataBooks 的问题还是我的问题?在执行此操作之前我需要对应用程序进行版本控制吗?只要我在模拟器中删除应用程序,我似乎就不必这样做。

有人知道如何在 CoreDataBooks 中添加第四个字符串属性(sqlite 字段)吗?

提前致谢!

【问题讨论】:

    标签: iphone xcode ios sqlite core-data


    【解决方案1】:

    我在 CoreDataBooks 中发现了这么好的一段代码

    NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"CoreDataBooks.sqlite"];
    /*
     Set up the store.
     For the sake of illustration, provide a pre-populated default store.
     */
    NSFileManager *fileManager = [NSFileManager defaultManager];
    // If the expected store doesn't exist, copy the default store.
    if (![fileManager fileExistsAtPath:storePath]) {
        NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"CoreDataBooks" ofType:@"sqlite"];
        if (defaultStorePath) {
            [fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
        }
    }
    

    CoreDataBooksAppDelegate.m:154 中,这对我来说意味着每次应用程序找不到 sqlite 文件时,它都会从未通过修改 CoreDataBooks.xcdatamodel。

    尝试使用其他方法,或者只是修改捆绑的 sqlite 文件。

    【讨论】:

    • 感谢您的回复!所以我可以只更改sqlite文件而不是通过.xcdatamodel添加属性?
    • 我尝试删除(或重命名)sqlite 文件,但应用程序没有按照我预期的更改重新创建 sqlite 文件“CoreDataBooks.sqlite”。
    【解决方案2】:

    如果应用没有加载,那么您的数据模型可能有问题。检查控制台以获取错误消息。

    你必须决定是使用 sqlite 还是 core-data。如果要监视 sqlite 文件的更改或向 sqlite 文件添加字段,则应使用 sqlite 而不是 core-data。

    如果你想使用核心数据,你应该忽略 sqlite 文件。

    【讨论】:

      【解决方案3】:

      想通了。应用程序正在创建新的 sqlite 文件,我只需要在模拟器文件中找到它。 Grrrr,当我正确地做所有事情时浪费了很多时间。我想吸取的教训:)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-10
        • 2013-05-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多