NSNnumber
特筆することは無いが、@(expression)には注意。
- //NSNumber literals
- //bool
- //NSNumber *boolYesValue = [NSNumber numberWithBool:YES];
- NSNumber *boolYesValue = @YES;
- NSLog(@"%@", boolYesValue); // 1
- //NSNumber *boolNoValue = [NSNumber numberWithBool:NO];
- NSNumber *boolNoValue = @NO;
- NSLog(@"%@", boolNoValue); // 0
- //char
- //NSNumber *charValue = [NSNumber numberWithChar:'Z'];
- NSNumber *charValue = @'Z';
- NSLog(@"%c", [charValue charValue]); // Z
- //double
- //NSNumber *doubleValue = [NSNumber numberWithDouble:3.14];
- NSNumber *doubleValue = @3.14;
- NSLog(@"%f", [doubleValue doubleValue]); //3.140000
- //float
- //NSNumber *floatValue = [NSNumber numberWithFloat:3.14F];
- NSNumber *floatValue = @3.14F;
- NSLog(@"%f", [floatValue floatValue]); //3.140000
- //signed int
- //NSNumber *intValue = [NSNumber numberWithInt:INT_MAX];
- NSNumber *intValue = @INT_MAX;
- NSLog(@"%d", [intValue intValue]); // 2147483647
- //unsigned int
- //NSNumber *uintValue = [NSNumber numberWithUnsignedInt:UINT32_MAX];
- NSNumber *uintValue = @UINT32_MAX;
- NSLog(@"%u", [uintValue unsignedIntValue]); // 4294967295
- //signed long
- //NSNumber *longValue = [NSNumber numberWithLong:LONG_MAX];
- NSNumber *longValue = @LONG_MAX;
- NSLog(@"%ld", [longValue longValue]); // 2147483647
- //unsigned long
- //NSNumber *ulongValue = [NSNumber numberWithUnsignedLong:LONG_MAX];
- NSNumber *ulongValue = @LONG_MAX;
- NSLog(@"%ld", [ulongValue unsignedLongValue]); // 2147483647
- //long long
- //NSNumber *longlongValue = [NSNumber numberWithLongLong:LONG_LONG_MAX];
- NSNumber *longlongValue = @LONG_LONG_MAX;
- NSLog(@"%lld", [longlongValue longLongValue]); // 9223372036854775807
- //expressions
- //NSNumber *expValue = [NSNumber numberWithInt:INT_MAX-1];
- NSNumber *expValue = @(INT_MAX-1);
- NSLog(@"%d", [expValue intValue]); // 2147483646
NSArray
配列のリテラル構文の場合、nilを指定することは出来ないことに注意。
また、リテラル構文で作成されるオブジェクトはimmutableなインスタンスなので、
値を変更したい場合はmutableCopyしてNSMutableArrayを取得する。
- //Array literals
- //NSArray *array = [NSArray arrayWithObjects:@1, @2, @3, nil];
- NSArray *array = @[@1, @2, @3];
- NSLog(@"%@", array); // (1, 2, 3)
- //添え字アクセス
- //NSNumber *number = [array objectAtIndex:0];
- NSNumber *number = array[0];
- NSLog(@"%@", number); // 1
- //添え字による値設定
- NSMutableArray *mutableArray = [@[@1, @2, @3] mutableCopy];
- mutableArray[0] = @10;
- NSLog(@"%@", mutableArray[0]); // 10
NSDictionary
リテラル構文の場合はインスタンスメソッドの順とは異なり、キー、値の順で指定することに注意。
mutableCopyの件は配列の時と一緒。
- //Dictionary literals
- //NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@1, @"one", @2, @"two", @3, @"three", nil];
- NSDictionary *dictionary = @{@"one" : @1,
- @"two" : @2,
- @"three" : @3};
- NSLog(@"%@", dictionary); // {one = 1; two = 2; three = 3;}
- //添え字によるアクセス
- NSNumber *numberValue = dictionary[@"one"];
- NSLog(@"%@", numberValue);
- //添え字による値設定
- NSMutableDictionary *mutableDictionary = [@{@"one" : @1, @"two" : @2, @"three" : @3} mutableCopy];
- mutableDictionary[@"one"] = @1.1;
- NSLog(@"%@", mutableDictionary[@"one"]); // 1.1
NSString
Cスタイルの型をNSStringへ変換することが@(expression)表現で可能になっている事に注意。
- //String literals
- NSString *stringValue = @"string value";
- NSLog(@"%@", stringValue);
- //CスタイルからのNSString生成
- static const char * const C_CONST_STRING = "CONST STRING";
- //NSString *c_string = [NSString stringWithCString:C_CONST_STRING encoding:NSUTF8StringEncoding];
- //NSString *c_string = [NSString stringWithUTF8String:C_CONST_STRING];
- NSString *c_string = @(C_CONST_STRING);
- NSLog(@"%@", c_string);
0 件のコメント :
コメントを投稿