LoginSignup
2
2

More than 3 years have passed since last update.

Objective-CからSwiftに書き換える[Array編]

Last updated at Posted at 2019-09-21

はじめに

Objective-Cのコードを書き換える時にArrayについてはハマるようです。
そこでTIPS的のようにできるのではないかと思い記事にします。

まず、追加している箇所のコードが何型の変数を追加しているのかで2パターンに分かれます。
パターン別にコード例を書きました。

Arrayの中身がDictionaryのとき

追加している箇所が、NSDictionaryの場合
(複数の値を1セットにして管理している場合)

方針

Swiftのタプルを使って置き換えるといいのではないかと考えました。

書き換え例

書き換え方の例をユースケース別に記載します。

宣言

Objective-C
@property (nonatomic) NSMutableArray *selectArray;
Swift
    typealias selectTuple = (index:Int , number:Int)
    var selectArray:[selectTuple] = []

初期化

Objective-C
    self.selectArray = [NSMutableArray array];
Swift
    selectArray = []

Swiftは宣言時に初期化しているので必要に応じて初期化を追加すること
(ViewController起動後1しか初期化しない場合はわざわざ初期化する必要はない)

追加

Objective-C
    NSDictionary *tappedDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                      [NSNumber numberWithInteger:inxdex], @"index",
                                      [NSNumber numberWithInteger:number], @"number",
                                      nil];
    [self.selectArray addObject:tappedDictionary];
Swift
        let tappedTuple:selectTuple = (index:index , number:numebr)
        selectArray.append(tappedTuple)

削除

Objective-C

    NSDictionary *tappedDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                      [NSNumber numberWithInteger:inxdex], @"index",
                                      [NSNumber numberWithInteger:number], @"number",
                                      nil];
    [self.selectArray removeObject:tappedDictionary];
Swift

        let tappedTuple:selectTuple = (index:index , number:numebr)
        if let index = selectArray.firstIndex(where: {$0.number == tappedTuple.number && $0.index == tappedTuple.index}) {
            selectArray.remove(at: index)
       }

参照

Objective-C
    for (NSDictionary *selectDictionary in self.selectArray) {
        NSInteger inxdex = [[selectDictionary objectForKey:@"index"] integerValue];
        NSInteger number = [[selectDictionary objectForKey:@"number"] integerValue];
    }


Swift
    for select in self.selectArray {
        let index = select.index
        let number = select.number
    }

Arrayの中身がNSStringなどのとき

追加している箇所が、NSString,UIImageなどの変数の場合

方針

SwiftではNSStringならStringの配列として置き換えるといいのではないかと考えました。

書き換え例

書き換え方の例をユースケース別に記載します。

宣言

Objective-C
@property (nonatomic) NSMutableArray *selectTextArray;
Swift
    private var selectTextArray:[String] = []

初期化

Objective-C
    self.selectTextArray = [NSMutableArray array];
Swift
    selectTextArray = []

Swiftは宣言時に初期化しているので必要に応じて初期化を追加すること
(ViewController起動後1しか初期化しない場合はわざわざ初期化する必要はない)

追加

Objective-C
    NSString *text = @"Hoge";
    [self.selectTextArray addObject:text];
Swift
        let text = "Hoge"
        selectTextArray.append(text)

削除

Objective-C
    NSString *text = @"Hoge";
    [self.selectTextArray removeObject:tappedText];
Swift
    let text = "Hoge"
    if let index = selectTextArray.firstIndex(of: text) {
        selectTextArray.remove(at: index)
    }

参照

Objective-C
    for (NSString *text in self.selectTextArray) {
        NSLog(@"%@",text);
    }


Swift
    for text in self.selectTextArray {
        print(text)
    }

最後に

Objective-CではできなかったことがSwiftではできます。
うまく活用して行きたいと思います。

2
2
0

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
2
2