macOS/iOS API解説

iOS , Mac アプリケーション開発のために使われる主要フレームワークの日本語情報です。2010年代に書かれた内容です。今後更新はありません。

目次

dictionaryWithObjects:forKeys:count:

Index>Foundation>NSDictionary

配列から辞書を作って返します
+(id)dictionaryWithObjects:(id *)objects
       forKeys:(id *)keys
       count:(NSUInteger)count

解説

配列から辞書を作って返します。
キーや値のオブジェクトがnilならば、NSInvalidArgumentExceptionが発生します。

返り値

( id )

作った辞書

引数

( id * )objects

オブジェクトの配列

( id * )keys

キーの配列
NSString *strArray[100];のように作成されます。

キーはコピーされて辞書に追加されます。

( NSUInteger )count

配列の要素数

クラス

NSDictionary

Class Methods

使用可能

10.0以降
iOS2.0以降

参照

- initWithObjects:forKeys:count:
+ dictionaryWithObject:forKey:
+ dictionaryWithObjects:forKeys:
+ dictionaryWithObjectsAndKeys:

例文

#pragma mark NSDictionary dictionaryWithObjects:forKeys:
-(void)method009
{
    static const int theCount = 100;
    
    NSString *keyArr[theCount]; 
    NSNumber *valArr[theCount];
    int i;
    for (i = 0; i < theCount; i++) {
        
        keyArr[i] = [NSString stringWithFormat:@"key%d",i];
        valArr[i] = [NSNumber numberWithInt:(100-i)];
    }
	
    NSDictionary *aDictionary = [NSDictionary dictionaryWithObjects:(id *)valArr 
                                          forKeys:(id *)keyArr count:theCount];
    
    NSLog(@"%s aDictionary %p = %@",__FUNCTION__,aDictionary,[aDictionary description]);
    //=>-[OOOAppDelegate method009] aDictionary 0x6e1f4f0 = {key0 = 100;key1 = 99;key10 = 90;......key98 = 2;key99 = 1;}
    
}