[Objective-C] 配列操作の覚え書き

2017年5月3日

Objective-Cの配列操作は少し複雑だけど、自分にとって分かりやすいページが無かったので、作っておきました。 一応、多言語を意識して項目を作ってみたので、その視点で参照してください。

Objective-Cにおける配列オブジェクトの種類

NSArray : 定義後の変更不可 NSMutableArray : 定義後の変更可能

配列の定義

固定値の定義

NSArray *datas = [NSArray arrayWithObjects: @"test-1" , @"test-2" , @"test-3" , @"test-4", nil];

空の配列を定義

NSMutableArray *mArray = [NSMutableArray array];

文字列を配列に分割(split)

# 日付を年月日に分割 NSString *dateValue = "2017/04/27"; NSArray *datas = [dateValue componentsSeparatedByString:@"/"]; # 年 NSLog(@"%@",[datas objectAtIndex:0]); # 月 NSLog(@"%@",[datas objectAtIndex:1]); # 日 NSLog(@"%@",[datas objectAtIndex:2]);

配列を結像して文字列に変換(join)

NSArray *datas = [NSArray arrayWithObjects: @"test-1" , @"test-2" , @"test-3" , @"test-4", nil]; NSString *str = [datas componentsJoinedByString:@"/"]; # str > test-1/test-2/test-3/test-4

配列の先頭を抜き出す(shift)

NSMutableArray *mArray = [NSMutableArray arrayWithObjects: @"test-1" , @"test-2" , @"test-3" , @"test-4", nil]; NSString *val = [mArray atIndex:0]; [mArray removeObjectAtIndex:0] # val > @"test-1" # mArray > [@"test-2" , @"test-3" , @"test-4"]

配列の先頭に追加(unshift)

NSMutableArray *mArray = [NSMutableArray arrayWithObjects: @"test-1" , @"test-2" , @"test-3" , @"test-4", nil]; [mArray insertObject:@"test-0" atIndex:0]; # mArray > [@"test-0" , @"test-1" , @"test-2" , @"test-3" , @"test-4"]

配列の最後について(push)

NSMutableArray *mArray = [NSMutableArray array]; [mArray addObject:@"ABC"];

配列の最後を抜き出す(pop)

NSMutableArray *mArray = [NSMutableArray arrayWithObjects: @"test-1" , @"test-2" , @"test-3" , @"test-4", nil]; NSString *val = [mArray lastObject]; [mArray removeLastObject]; # val > @"test-4" # mArray > [@"test-1" , @"test-2" , @"test-3"]

配列の任意の箇所に要素を追加(splice)

NSMutableArray *mArray = [NSMutableArray arrayWithObjects: @"test-1" , @"test-2" , @"test-3" , @"test-4", nil]; [mArray insertObject:@"test-0" atIndex:1]; # > [@"test-1" , @"test-0" , @"test-2" , @"test-3" , @"test-4"] [mArray replaceObjectAtIndex:1 withObject:@"XYZ"]; # > [@"test-1" , @"XYZ" , @"test-0" , @"test-2" , @"test-3" , @"test-4"]

2つの配列の結合

NSMutableArray *mArray1 = [NSMutableArray arrayWithObjects: @"test-1" , @"test-2" , nil]; NSMutableArray *mArray2 = [NSMutableArray arrayWithObjects: @"test-3" , @"test-4" , nil]; [mArray1 addObjectsFromArray : mArray2]; # mArray1 > [@"test-1" , @"test-2" , @"test-3" , @"test-4"]

配列の要素数を取得

int cnt = [datas count];

指定位置の要素を取得

NSString *val = [datas objectAtIndex : 2];

配列の一番最後の要素を取得

NSString *val = [datas lastObject];

2つの配列が同じであるか判定

BOOL bl = [arr1 isEqualToArray arr2];

配列内にある要素の確認

NSArray *datas = [NSArray arrayWithObjects: @"test-1" , @"test-2" , @"test-3" , @"test-4", nil]; BOOL bl = [datas containsObject:@"test-2"];

配列内の要素文字列に含まれているか確認

BOOL bl = [datas containsObject:@"test"];

配列内の要素内容を元に要素番号を取得

NSArray *datas = [NSArray arrayWithObjects: @"test-1" , @"test-2" , @"test-3" , @"test-4", nil]; int indexNum = [datas indexOfObject:@"test-2"]; # > indexNum = 1

このブログを検索

ごあいさつ

このWebサイトは、独自思考で我が道を行くユゲタの少し尖った思考のTechブログです。 毎日興味がどんどん切り替わるので、テーマはマルチになっています。 もしかしたらアイデアに困っている人の助けになるかもしれません。