2014年3月11日火曜日

Objective-C リテラル構文備忘録(Objective-C Literals)

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

0 件のコメント :

コメントを投稿