0

我正在尝试在表格视图中显示一些数据,该表格视图是选项卡栏控制器中视图控制器的一部分。目前我正在尝试在 iPhone Simulator 上运行该应用程序。我将 sqlite 数据库复制到以下位置 - /Users/{username}/Library/Application Support/iPhoneSimulator/4.2/Applications/{appid}/Documents

现在我试图在viewWillAppear我的视图控制器的方法中获取数据。

#import "FugitivesViewController.h"    
#import "MyAppDelegate.h"

#import "Fugitive.h"

- (void)viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];
        NSManagedObjectContext *context = [(MyAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
        NSFetchRequest *request = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"Fugitive" inManagedObjectContext:context];
        [request setEntity:entity];

        NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
        NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
        [request setSortDescriptors:sortDescriptors];
        [sortDescriptors release];
        [sortDescriptor release];

        NSError *error = nil;
        NSMutableArray *mutableFetchResults = [[context executeFetchRequest:request error:&error] mutableCopy];

        if (mutableFetchResults == nil) {
            NSLog(@"Error while fetching the results");
        }

        self.items = mutableFetchResults;

        [mutableFetchResults release];
        [error release];

        [request release];
        [context release];
    }

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [self.items count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    Fugitive *fugitive = [items objectAtIndex:indexPath.row];
    cell.textLabel.text = fugitive.name;

    return cell;
}

问题是这在视图控制器中什么也没有显示,也没有错误。我已经在标签栏控制器中连接了所需的插座。

检查调试器,可变数组显示 0 个对象。我刚刚开始使用 iOS 开发。有人可以帮我弄清楚这里可能出了什么问题吗?

更新 - 在Yuji的评论的帮助下,我检查了 iPhone Simulator 的 Documents 文件夹中复制的文件。它没有任何数据。这就是视图没有显示数据的原因。因此,问题似乎出在我用来将文件从项目文件夹复制到应用程序的文档文件夹的代码中。

这是怎么回事....

// MyAppDelegate.m
#import "MyAppDelegate.h"

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    // Override point for customization after application launch.
    [self createEditableCopyOfDatabaseIfNeeded];

    [self.window addSubview:tabcontroller.view];
    [self.window makeKeyAndVisible];

    return YES;
}

- (void)createEditableCopyOfDatabaseIfNeeded {
    NSString *defaultDirectory = [[self applicationDocumentsDirectory] absoluteString];
    NSString *writableDBPath = [defaultDirectory stringByAppendingPathComponent:@"iBountyHunder1.sqlite"];

    NSFileManager *fileManager = [NSFileManager defaultManager];
    if (![fileManager fileExistsAtPath:writableDBPath]) {
        NSString *defaultDBPath = [[NSBundle mainBundle] pathForResource:@"iBountyHunder" ofType:@"sqlite"];
        if (defaultDBPath) {
            NSError *error;
            BOOL success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
            if (!success) {
                NSLog(@"The error is %@", [error localizedDescription]);
            }
        }
    }
}

不明白为什么这不起作用????

4

5 回答 5

1

我刚刚注意到您在上面的代码片段之一中将您的数据库命名为“iBountyHunder”以及“iBountyHunder1”。Head First 提供的文件是“iBountyHunter”(最后是 t,而不是 d)。

您可能需要检查您的项目名称是否与您的 db 文件名一致,因为核心数据模板希望它们相同。

当我让数据库工作时,我还发现每次使用 Build -> Clean All Targets 以及 iPhoneSimulator -> Reset Contents and Settings 都有帮助...

于 2011-02-28T16:12:55.713 回答
1

尝试遵循书中的代码时,我遇到了同样的错误。检查可写路径上的可用数据库并检查两个.sqlite文件的内容后,我发现数据库的名称应该与项目名称匹配。查看您的导入语句,看起来您将项目命名为My. 所以数据库应该被复制My.sqlite为文件名。我已经在我的 macbook 上试过了,它可以工作。

仅供参考,我sqlite database browser用来检查 .sqlite 文件的内容。

于 2011-08-02T16:37:46.567 回答
1

是不是自己把sqlite3文件复制到Documents目录下的?如果在那里找不到,该代码将创建可写的数据库副本到 Documents 目录。

请执行下列操作。

  1. 在 Xcode 中运行项目
  2. 按下 iPhone 模拟器上的主页按钮
  3. 移除 iBountyHunter
  4. 退出模拟器
  5. 再次运行项目
于 2011-02-09T12:14:36.247 回答
0

您的课程以 开头#import "MyAppDelegate.h",这让我怀疑它是您的应用程序委托而不是视图控制器。如果它不是视图控制器,那么我认为- (void)viewWillAppear:(BOOL)animated不会运行。尝试NSLog(@"this code is being run");在该方法中添加一个以查看它是否正在执行。

于 2011-01-20T02:48:45.603 回答
0

我下载了第 7 章的代码示例。我在 XCode 中打开它并将 Project->Edit Project Settings->Base SDK 更新为最新的 sdk (4.2)。如果计划是从头开始创建一个新项目,请确保它由 Core Data 支持,并且他们项目中的代码被正确复制和粘贴。

于 2011-01-20T03:02:04.260 回答