LoginSignup
9
10

More than 5 years have passed since last update.

Objective-C Tips

Last updated at Posted at 2014-10-29

とりあえず、忘れないように、、メモとして残しておきます。
Xcode4.4から有効。

NSArray, NSDictionary, NSNumberのモダンな書き方

NSArray

インスタンスの生成

sample
// old
NSArray *oldArray = [NSArray arrayWithObjects:@"value1", @"value2", nil];
NSMutableArray *oldMutableArray = [NSMutableArray arrayWithObjects:@"value3", @"value4", nil];

// new
NSArray *newArray = @[@"value1", @"value2"];
NSMutableArray *newMutableArray = [@[@"value3", @"value4"] mutableCopy];

要素の取得

sample

NSArray *array = @[@"value1", @"value2", @"value3", @"value4"];

// old
NSString *item = [array objectAtIndex:0];

// new 
NSString *newItem = array[0];

要素の置き換え

sample

NSMutableArray *newMutableArray = [@[@"value3", @"value4"] mutableCopy];

// old
[newMutableArray replaceObjectAtIndex:0 withObject:@"replace1"];

// new 
newMutableArray[0] = @"replace1";

NSDictionary

インスタンスの生成

sample

    // old
    NSDictionary *oldDict = [NSDictionary dictionaryWithObjectsAndKeys:
                            @"value1", @"key1",
                            @"value2", @"key2",
                            nil];

    NSMutableDictionary *oldMutableDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                          @"value3", @"key3",
                                          @"value4", @"key4",
                                          nil];

    // new
    NSDictionary *newDict = @{
                              @"key1":@"value1",
                              @"key2":@"value2"
                             };

    NSMutableDictionary *newMutableDict = [@{
                                             @"key3":@"value3",
                                             @"key4":@"value4",
                                            } mutableCopy];

要素の取得

sample

    NSDictionary *dict = @{
                           @"key1":@"value1",
                           @"key2":@"value2"
                          };


    // old
    NSString *oldKey1Item = [dict objectForKey:@"key1"];

    // new
    NSString *newKey1Item = dict[@"key1"];

要素の追加、置換

sample
    NSMutableDictionary *mutableDict = [@{
                                             @"key3":@"value3",
                                             @"key4":@"value4",
                                            } mutableCopy];
    // old
    [mutableDict setObject:@"update1" forKey:@"key1"];

    // new
    mutableDict[@"key1"] = @"update1";

NSNumber

sample

    // old
    NSNumber *oldInt = [NSNumber numberWithInt:100];
    NSNumber *oldUInt = [NSNumber numberWithUnsignedInt:20U];

    NSNumber *oldLong = [NSNumber numberWithLong:200L];
    NSNumber *oldLongLong = [NSNumber numberWithLong:30LL];

    NSNumber *oldFloat = [NSNumber numberWithFloat:1.2f];
    NSNumber *oldDouble = [NSNumber numberWithDouble:1.1];

    NSNumber *oldBool = [NSNunber numberWithBool:YES];

    NSNumber *oldSum = [NSNumber numberWithInt:2+2];

    // new 
    NSNumber *newInt = @100;
    NSNumber *newUInt = @20U;

    NSNumber *newLong = @200L;
    NSNumber *newLongLong = @30LL;

    NSNumber *newFloat = @1.2f;
    NSNumber *newDouble = @1.1;

    NSNumber *newBool = @YES;

    NSNumber *newSum = @(2+2);


Tips

2014/12/09更新
@sashoさんご指摘ありがとうございます。

int → NSString

↓いつもはこんな感じで書いてます。

sample
    NSString *test = [NSString stringWithFormat:@"%d",1];

色々書いてたら、なんかこんなのもの出来た的なメモ

sample
    NSString *test = [@1 stringValue];

NSIntegerやらintをNSStringにキャストしなくちゃいけない所が出てきて、
stringWithFormatを書くのが長ったらしいので、何か方法はないかと探してみた。

結果的に記述としては短くなったけれども、intからNSNumberにキャストした後にNSStringにしてるのでちょっと無駄かな。
素直にstringWithFormatを使ったほうがいいのだろうか・・。

9
10
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
10