0

タブバーコントローラーのビューコントローラーの一部であるテーブルビューにいくつかのデータを表示しようとしています。現在、iPhoneシミュレーターでアプリを実行しようとしています。sqliteデータベースを次の場所にコピーしました-/Users/ {username} / Library / Application Support / iPhoneSimulator / 4.2 / Applications / {appid} / Documents

viewWillAppear現在、ViewControllerのメソッドでデータをフェッチしようとしています。

#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;
}

問題は、これがView Controllerに何も表示されず、エラーも表示されないことです。タブバーコントローラーに必要なコンセントを接続しました。

デバッガーをチェックインすると、可変配列は0個のオブジェクトを示します。iOSの開発を始めたばかりです。誰かがここで何がうまくいかないのかを理解するのを手伝ってくれますか?

更新-Yujiのコメントの助けを借りて、私はiPhoneSimulatorの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

自分でsqlite3ファイルをDocumentsディレクトリにコピーしましたか?コードがドキュメントディレクトリに書き込み可能なデータベースコピーを見つけられない場合は、そこに作成します。

以下をせよ。

  1. Xcodeでプロジェクトを実行します
  2. iPhoneシミュレータのホームボタンを押す
  3. iBountyHunterを削除します
  4. シミュレータを終了します
  5. プロジェクトを再実行します
于 2011-02-09T12:14:36.247 に答える
1

上記のコードスニペットの1つで、データベースに「iBountyHunder」と「iBountyHunder1」という名前を付けていることに気づきました。Head Firstによって提供されるファイルは「iBountyHunter」です(最後のdではなくt)。

コアデータテンプレートではプロジェクト名が同一であると想定されているため、プロジェクト名がdbファイル名と一致していることを確認することをお勧めします。

dbを機能させていたとき、[ビルド]->[すべてのターゲットをクリーンアップ]および[iPhoneSimulator]->[コンテンツと設定をリセット...]を使用するたびに役立つこともわかりました。

于 2011-02-28T16:12:55.713 に答える
1

本のコードをたどろうとすると、同じエラーが発生しました。書き込み可能なパスで使用可能なデータベースを確認し、両方の.sqliteファイルの内容を確認したところ、データベースの名前がプロジェクト名と一致している必要があることがわかりました。importステートメントを見ると、プロジェクトに。という名前を付けているようですMy。したがって、データベースはMy.sqliteファイル名としてコピーする必要があります。私は自分のMacBookでそれを試しましたが、動作します。

sqlite database browser参考までに、私は.sqliteファイルの内容をチェックするために使用します。

于 2011-08-02T16:37:46.567 に答える
0

クラスはで始まり#import "MyAppDelegate.h"ます。これは、ビューコントローラではなく、アプリのデリゲートであると思われます。ビューコントローラでない場合は、- (void)viewWillAppear:(BOOL)animated実行されないと思います。NSLog(@"this code is being run");そのメソッドにを入れて、実行されているかどうかを確認してください。

于 2011-01-20T02:48:45.603 に答える
0

第7章のコードサンプルをダウンロードしました。XCodeで開き、[プロジェクト]->[プロジェクト設定の編集]->[ベースSDK]を最新のSDK(4.2)に更新しました。新しいプロジェクトを最初から作成する計画の場合は、Core Dataに支えられており、プロジェクトのコードが正しくコピーされて貼り付けられていることを確認してください。

于 2011-01-20T03:02:04.260 に答える