私はiphoneを初めて使用します。plistファイルからデータを取得する際に問題が発生しました。実際、私のplistファイルは次のようになっています。
キータイプ値ジェネシス辞書(0項目)出エジプト記(0項目)レビ記辞書(0項目)
ここで、plistファイル内の辞書の数を取得するにはどうすればよいですか?これの出力は3です。
誰かがこれを知っているなら私を助けてください...
私はiphoneを初めて使用します。plistファイルからデータを取得する際に問題が発生しました。実際、私のplistファイルは次のようになっています。
キータイプ値ジェネシス辞書(0項目)出エジプト記(0項目)レビ記辞書(0項目)
ここで、plistファイル内の辞書の数を取得するにはどうすればよいですか?これの出力は3です。
誰かがこれを知っているなら私を助けてください...
簡単な回答ですが、質問への回答を実際に受け入れていただければ幸いです。
NSDictionary *dictionary = ....
int count = [[dictionary allKeys] count]; // Will return 3. allKeys will contain (Genesis,Exodus,Leviticus)
plistはディクショナリまたは配列を返します。次に、plistに含まれるディクショナリの数を知るために、ディクショナリオブジェクトでカウントメッセージを使用できます。
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);
これはどう:
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]];