0

次のように、辞書のplist配列をデバイスのドキュメントディレクトリにコピーします。

AppDelegate.m:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self performSelector:@selector(copyPlist)];
    return YES;
}

- (void)copyPlist {

    NSError *error;

    NSFileManager *fileManager=[NSFileManager defaultManager];
    NSArray *pathsArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

    NSString *doumentDirectoryPath=[pathsArray objectAtIndex:0];
    NSString *destinationPath= [doumentDirectoryPath stringByAppendingPathComponent:@"Wine.plist"];

    if ([fileManager fileExistsAtPath:destinationPath]){
        NSLog(@"database localtion %@",destinationPath);
        return;
    }

    NSString *sourcePath= [[NSBundle mainBundle] pathForResource:@"Wine" ofType:@"plist"];
    NSLog(@"source path %@",sourcePath);
    [fileManager copyItemAtPath:sourcePath toPath:destinationPath error:&error];
    }

データベースの場所のログ:

/var/mobile/Applications/832E16F4-A204-457E-BFF0-6AEA27915C25/Documents/Wine.plist

次に、plistにアクセスし、sortedWines配列に辞書を入力して、TableViewにデータを入力しようとしています。

TableViewController.h

#import <UIKit/UIKit.h>

@interface WinesViewController : UITableViewController <UIActionSheetDelegate> {
     NSMutableArray *sortedWines;
}

@end

TableViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Wine.plist"];
    NSLog(@"plist path %@", path);

    sortedWines = [[NSMutableArray alloc] initWithContentsOfFile:path];
    NSLog(@"objects %@", sortedWines);


    NSSortDescriptor * sortDesc = [[NSSortDescriptor alloc] initWithKey:@"Popularity" ascending:YES];
    [sortedWines sortUsingDescriptors:[NSArray arrayWithObject:sortDesc]];

    [super viewDidLoad];
}

plistパスのログ:

/var/mobile/Applications/832E16F4-A204-457E-BFF0-6AEA27915C25/Documents/Wine.plist

およびオブジェクトのログ:

(ヌル)

今すぐsortedWines配列をオブジェクトで埋める方法は?

4

1 に答える 1

0

したがって、解決策は、バンドル内の元のplistファイルが辞書の配列として構造化されていないことです。

このアサーションを解決するには、copyPlistメソッドにさらに数行追加します。

NSString *sourcePath= [[NSBundle mainBundle] pathForResource:@"Wine" ofType:@"plist"];
NSLog(@"source path %@",sourcePath);
NSMutableArray *sortedWines = [[NSMutableArray alloc] initWithContentsOfFile:sourcePath];
NSLog(@"objects %@", sortedWines); // bet this is nil too

編集

したがって、上記のファイルがあることがわかったら、表示/テストする必要があります

    [fileManager copyItemAtPath:sourcePath toPath:destinationPath error:&error];

ここでの問題は、リターンコードをチェックせず、やみくもに成功したと想定することです。したがって、リターンコードを見て、エラーをログに記録すると失敗します。それが機能していることがわかったら、コピーの直後に別の行を追加して、viwDidLoadと同じようにファイルを配列にロードします(コードをコピーします)。

それは機能するが、viewDidLoadでファイルが欠落している場合、唯一の可能性はあなたかシステムがドキュメントディレクトリ内のすべてのファイルを削除していることです(アプリがiCloudを使用している場合、このフォルダのルールが変更されると聞きました。

PS:なぜあなたはこれをしているのですか:

[self performSelector:@selector(copyPlist)];

それ以外の:

[self copyPlist];
于 2012-08-11T23:00:11.980 に答える