0

私はiphoneを初めて使用します。plistファイルからデータを取得する際に問題が発生しました。実際、私のplistファイルは次のようになっています。

キータイプ値ジェネシス辞書(0項目)出エジプト記(0項目)レビ記辞書(0項目)

ここで、plistファイル内の辞書の数を取得するにはどうすればよいですか?これの出力は3です。

誰かがこれを知っているなら私を助けてください...

4

4 に答える 4

1

簡単な回答ですが、質問への回答を実際に受け入れていただければ幸いです。

NSDictionary *dictionary = ....
int count = [[dictionary allKeys] count]; // Will return 3. allKeys will contain (Genesis,Exodus,Leviticus)
于 2012-05-31T14:13:33.053 に答える
0

plistはディクショナリまたは配列を返します。次に、plistに含まれるディクショナリの数を知るために、ディクショナリオブジェクトでカウントメッセージを使用できます。

于 2012-05-31T14:05:32.140 に答える
0
NSString *path = [[NSBundle mainBundle] pathForResource:@"yourResource" ofType:@"plist"];

NSFileManager *fileManager = [NSFileManager defaultManager];

if ([fileManager fileExistsAtPath:path]) 
{
    NSLog(@"The file exists");
} 
else 
{
    NSLog(@"The file does not exist");
}

NSMutableDictionary *myDic = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
int count = [myDic count];
NSLog(@"%d",count);
于 2012-05-31T14:12:03.863 に答える
0

これはどう:

NSArray *myPlistPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                     NSUserDomainMask, YES); 
NSString *documentsDirectory = [myPlistPath objectAtIndex:0]; 
NSString *myPath = [documentsDirectory
                         stringByAppendingPathComponent:@"MYPLIST.plist"]; 
NSDictionary *plistDictionary = [[NSDictionary alloc] initWithContentsOfFile:myPath]; 

NSArray *array = [[plistDictionary objectForKey:0]
                      sortedArrayUsingSelector:@selector(compare:)]; 

return [NSString stringWithFormat:@"%d MyPlist", [plistDictionary count]];
于 2012-05-31T14:24:55.780 に答える