質問

ったデータ構造を用いた手動の

NSDictionary* league1 = [[NSDictionary alloc] initWithObjectsAndKeys: @"Barclays Premier League", @"name",
                 @"Premier League", @"shortname",
                 @"101", @"id", nil];
NSDictionary* league2 = [[NSDictionary alloc] initWithObjectsAndKeys: @"Coca-Cola Championship", @"name",
                 @"Championship", @"shortname",
                 @"102", @"id", nil];
NSDictionary* league3 = [[NSDictionary alloc] initWithObjectsAndKeys: @"Scottish Premier League", @"name",
                 @"SPL", @"shortname",
                 @"201", @"id", nil];
NSDictionary* league4 = [[NSDictionary alloc] initWithObjectsAndKeys: @"Champions League", @"name",
                 @"UCL", @"shortname",
                 @"501", @"id", nil];

contentArray = [[NSArray alloc] initWithObjects:

                [[NSDictionary alloc] initWithObjectsAndKeys: @"English", @"category", [[NSArray alloc] initWithObjects: league1, league2, nil], @"leagues", nil],
                [[NSDictionary alloc] initWithObjectsAndKeys: @"Scottish", @"category", [[NSArray alloc] initWithObjects: league3, nil], @"leagues", nil],
                [[NSDictionary alloc] initWithObjectsAndKeys: @"Tournaments", @"category", [[NSArray alloc] initWithObjects: league4, nil], @"leagues", nil],
                nil];

[league1 release];
[league2 release];
[league3 release];
[league4 release];

しかし、これってすごいことだなと思いいでしたから読み込んだファイルです。で作成したファイルのリーグをいう。plistは以下の構造

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <array>
        <dict>
            <key>category</key>
            <string>English</string>
            <key>leagues</key>
            <array>
                <dict>
                    <key>name</key>
                    <string>Barclays Premier League</string>
                    <key>shortname</key>
                    <string>Premier League</string>
                    <key>id</key>
                    <string>101</string>
                </dict>
                <dict>
                    <key>name</key>
                    <string>Coca-Cola Championship</string>
                    <key>shortname</key>
                    <string>Championship</string>
                    <key>id</key>
                    <string>102</string>
                </dict>
            </array>
        </dict>
        <dict>
            <key>category</key>
            <string>Scottish</string>
            <key>leagues</key>
            <array>
                <dict>
                    <key>name</key>
                    <string>Scottish Premier League</string>
                    <key>shortname</key>
                    <string>SPL</string>
                    <key>id</key>
                    <string>201</string>
                </dict>
            </array>
        </dict>
        <dict>
            <key>category</key>
            <string>Tournaments</string>
            <key>leagues</key>
            <array>
                <dict>
                    <key>name</key>
                    <string>Champions League</string>
                    <key>shortname</key>
                    <string>UCL</string>
                    <key>id</key>
                    <string>501</string>
                </dict>
            </array>
        </dict>
    </array>
</plist>

どのようなこのファイルを読み込みます。しかし様々な方法がない。私は全く身に覚えがないので知り合いの場所にファイルです。参考とっては、以下のいずれかの方法が

NSString* errorDesc = nil;
NSPropertyListFormat format;
NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"league" ofType:@"plist"];
NSData* plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
contentArray = (NSArray*)[NSPropertyListSerialization
                                     propertyListFromData:plistXML
                                     mutabilityOption:NSPropertyListMutableContainersAndLeaves
                                     format:&format
                                     errorDescription:&errorDesc];

if (!contentArray) {
    NSLog(errorDesc);
    [errorDesc release];
}

または

NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"league" ofType:@"plist"];
contentArray = [NSArray arrayWithContentsOfFile:plistPath];

または

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];

NSString *fooPath = [documentsPath stringByAppendingPathComponent:@"leagues.plist"];
NSLog(fooPath);
contentArray = [NSArray arrayWithContentsOfFile:fooPath];
NSLog(@"%@",contentArray);

これはその駆動く完全に非常識だ。助けてください!

何卒よろしくお願いいたし

役に立ちましたか?

解決

NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"league" ofType:@"plist"];
contentDict = [NSDictionary dictionaryWithContentsOfFile:plistPath];

その答えは正しいです - あなたのファイルがアプリにあることを確信していますか?プロジェクトに追加し、アプリバンドルにコピーされるかどうかを確認しましたか?そうでない場合、それはあなたが構築しているターゲットにファイルが追加されていないかもしれません。特に複数のターゲットがある場合は特に簡単に犯す間違いです。

他のヒント

このコードを使用します plist プロジェクトのリソースフォルダーにあります。

  NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"league"    ofType:@"plist"];
 contentArray = [NSArray arrayWithContentsOfFile:sourcePath];

場合 plist アプリのドキュメントディレクトリ内にありますこれを使用してください。

    NSArray *paths = 
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;

NSString *plistName = @"league";
NSString *finalPath = [basePath stringByAppendingPathComponent: 
                       [NSString stringWithFormat: @"%@.plist", plistName]];



contentArray = [NSArray arrayWithContentsOfFile:finalPath];

私はこの問題を抱えていましたが、それが辞書であるべきだった配列にプリストの結果を配置していたので、それは機能していませんでした。

NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"VehicleDetailItems" ofType:@"plist"];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:plistPath];

ケンドールは正しいです。

私の経験では、Xcodeの「Resources」フォルダーにファイルを追加する必要があります。

のための完全性、ケンダルインターナショナルとbentfordは全く正しい。しかし、私のサンプルcontentArrayたは財産の方法での範囲で arrayWithContentsOfFile を作成し、オートリオブジェクトです。

この正常に動作しまう3つのもの

  1. のファイルのフォルダ

  2. 名前のファイルが正しく(たリーグをいう。plistの代わりに。plist)

  3. ファイルの読み込みを使用 [[NSArray alloc]initWithContentsOfFile:plistPath)];

第三部分を割り当てNSArrayないリリースを抜けると、この機能---もちろん、これが必要に発売されdeallocます。

追加するだけです。私は同じ問題を抱えており、提案された解決策は問題を解決するのに役立ちましたが、実際に正確なソリューションを使用したかどうかはわかりません。私の場合、問題は、.plistファイルが別のターゲットに追加されたことでした(少し前に新しいターゲットを追加しました)。したがって、ソリューションは.plist> get info>ターゲットを取得し、正しいターゲットに追加されていることを確認して、インストール時にデバイスにコピーされます。ダーンは、すぐにそれを理解していたので、私は多くの時間を節約できたでしょう。これも役立つことを願っています。よろしく!

ファイルがリソースフォルダーに追加されておらず、ドキュメントディレクトリにのみ配置されている場合。あなたはこれを行うことができます

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"file.plist"];
NSMutableArray *arrContentsplist = [[NSMutableArray alloc] initWithContentsOfFile:path];

このコードは機能しています

NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"plist-name" ofType:@"plist"];
NSDictionary *Dictionary= [[NSDictionary alloc]initWithContentsOfFile:plistPath];
NSLog(@" current version CFBundleVersion = %@",[Dictionary valueForKey:@"CFBundleVersion"]);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top