NSDictionaryについて

Konton's iPhone application labolatory

English top page

NSDictionaryとは

NSDictionaryはNSArray同様にオブジェクトの集合を格納するものですが、NSArrayとは管理の方法が違います。 NSArrayはそれぞれのオブジェクトに順番に番号をつけて管理しますが、こちらはキーと呼ばれる名前をそれぞれのオブジェクトに結びつけた状態で管理します。 キーはNSStringです。オブジェクト全体で一つのものの状態を表す、というような使われ方をします。 ファイルの持つ様々な情報(ファイルサイズなど)をこのオブジェクトで一まとめにして受け取るというような使われ方をします。


代表的なメソッド(目的別)

ファイルにNSDictionaryの内容を保存したい - writeToFile:atomically:

ファイルの内容からNSDictionaryオブジェクトを作りたい - initWithContentsOfFile:

キーに対応したオブジェクトを取得したい - objectForKey:

ファイルのサイズを取得したい - fileSize

中にあるオブジェクトの内容をログ出力したい - description


ファイルにNSDictionaryの内容を保存したい - writeToFile:atomically:

- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag

現在のNSDictionaryをファイルにして保存します。以下の例では作ったNSDictionaryを標準で作られるアプリケーション用のドキュメントディレクトリに保存しています。 ファイルの中身はinfo.plist等と同じxml形式なので、この例では拡張子をplistにしています。 常識的に考えて、ドキュメントディレクトリが作られていないということは無いはずなので、特にエラー対策はしていません。attomicallyをYESにすると、 一度内容をファイルを別名で書き出し、その後で改名してその名前のファイルがある場合はそれを上書きします。 NOの場合は直接その名前で書き出します。

NSArray *objectArray = [[NSArray alloc] initWithObjects:@"Aさん",[NSNumber numberWithInt:50],nil];
NSArray *keyArray = [[NSArray alloc] initWithObjects:@"名前",@"年齢",nil];
NSDictionary *dictionary = [[NSDictionary alloc] initWithObjects:objectArray forKeys:keyArray];
[objectArray release];
[keyArray release];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"dictionarydata.plist"];
[dictionary writeToFile:dataPath atomically:YES];

ファイルの内容からNSDictionaryオブジェクトを作りたい - initWithContentsOfFile:

- (id)initWithContentsOfFile:(NSString *)aPath

ファイルに保存したNSArrayの情報をもとにしてNSArrayを作ります。以下の例では標準で作られるアプリケーション用のドキュメントディレクトリにあるファイルから、 作成したNSArrayであるitemsに保存されていたNSArrayの情報をファイルから読み込んでいます。私はファイルの拡張子に定番のものがあるのかは知りませんが、 ファイルの中身はxml形式なので、この例では拡張子をxmlにしています。一応ファイルが存在しないときに実行しないようにエラー対策をしています。

NSDictionary *dictionary;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:"dictionarydata.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL success = [fileManager fileExistsAtPath:dataPath];
if(success) {
   dictionary = [[NSDictionary alloc] initWithContentsOfFile:dataPath];
}

キーに対応したオブジェクトを取得したい -  objectForKey:

- (id)objectForKey:(id)aKey

オブジェクトに対応するキーを使ってNSDictionaryに格納されているオブジェクトを得ます。 以下の例では、自分のアプリケーションで自動的に作られるドキュメントディレクトリに保存されている、 "mydictionary.plist"というファイルのファイルサイズをobjectForKeyを使って取得しています。 ファイルサイズはNSNumberオブジェクトに格納されているので、単純なunsigned long long型の数値にするには、 NSNumberのunsignedLongLongValueメソッドを使います。この例ではファイルが無かった場合などのエラーチェックは特にしていません。

NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"dictionary.plist"];
NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:dataPath error:nil];
unsigned long long fileSize = [[fileAttributes objectForKey:NSFileSize]unsignedLongLongValue];

ファイルのサイズを取得したい - fileSize

- (unsigned long long)fileSize

NSDictionaryにファイルの情報を格納させている場合に、ファイルサイズを得られます。 以下の例では、自分のアプリケーションで自動的に作られるドキュメントディレクトリに保存されている、 "mydictionary.plist"というファイルのファイルサイズをfileSizeを使って取得しています。 上のobjectForKeyを使ったときとは異なり、直接unsigned long long型の数値が得られます。 この例ではファイルが無かった場合などのエラーチェックは特にしていません。

NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"dictionary.plist"];
NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:dataPath error:nil];
unsigned long long fileSize = [fileAttributes fileSize];

中にあるオブジェクトの内容をログ出力したい - description

- (NSString *)description

現在のNSArrayに格納されている情報を全て文字列にします。中にあるNSArrayやNSDictionaryも文字列にされます。 デバッグ時のログ出力のためによく使われるメソッドです。 キーの文字列とそのキーで格納されているオブジェクトが文字で出力されてきますが、どんな型であるかまではわかりません。 この例ではファイルが無かった場合などのエラーチェックは特にしていません。

NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"dictionary.plist"];
NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:dataPath error:nil];
NSLog(@"fileAttributes:%@",[fileAttributes description]);
前へ次へ
Copyright© 2009 Konton All rights reserved. - このサイトについて - サイトマップ

Valid XHTML 1.1 正当なCSSです!