iOSエンジニアのつぶやき

毎朝8:30に iOS 関連の技術について1つぶやいています。まれに釣りについてつぶやく可能性があります。

Unable to infer complex closure return type; add explicit type to disambiguateの対処方

本日はSwift初学者向けの Unable to infer complex closure return type; add explicit type to disambiguate の対処法を簡単に紹介します🍺

結論

これはクロージャの戻り値の方をSwiftコンパイラが上手く型推論できないときに発生します。

        let itemLayouts = $0.items.map { item in // Unable to infer complex closure return type; add explicit type to disambiguate
            let layoutItem = NSCollectionLayoutItem(layoutSize: .init(widthDimension: .fractionalWidth(1.0), heightDimension: .fractionalWidth(1.0)))
            layoutItem.contentInsets = .init(top: 10, leading: 12, bottom: 10, trailing: 12)
            return layoutItem
        }

ということで、いずれかの方法で型を明確に宣言してあげましょう🏃‍♂️

まずは、変数に宣言するパターン。

        let itemLayouts: [NSCollectionLayoutItem] = $0.items.map { item in
            let layoutItem = NSCollectionLayoutItem(layoutSize: .init(widthDimension: .fractionalWidth(1.0), heightDimension: .fractionalWidth(1.0)))
            layoutItem.contentInsets = .init(top: 10, leading: 12, bottom: 10, trailing: 12)
            return layoutItem
        }

mapの戻り値を明確に宣言するパターン。

        let itemLayouts = $0.items.map { item -> NSCollectionLayoutItem in
            let layoutItem = NSCollectionLayoutItem(layoutSize: .init(widthDimension: .fractionalWidth(1.0), heightDimension: .fractionalWidth(1.0)))
            layoutItem.contentInsets = .init(top: 10, leading: 12, bottom: 10, trailing: 12)
            return layoutItem
        }

てな感じで本日も以上となります🍺

その他の記事

yamatooo.blog

yamatooo.blog

yamatooo.blog

【Xcode】ファイル内のメソッド検索

本日は初学者向けのXcodeの小ネタです👷‍♀️(ネタが、、)

ファイル内のメソッド検索

ファイル内の特定のメソッドなどを検索する時は、⌘ + Fで Findナビゲーションを開いてもいいですが、ファイルナビゲーションからメソッドを簡単に探すこともできます。

f:id:yum_fishing:20210809192051p:plain

ここからメソッドを下記のように検索できます。

f:id:yum_fishing:20210809192132p:plain

という感じで短いですが、本日も以上となります🍺

その他の記事

yamatooo.blog

yamatooo.blog

yamatooo.blog

【Next】Linkを持つbuttonってどうやって作る

結論

普通のリンクの場合は、next/linkを使用して下記のように実装できます。

import Link from 'next/link'


<Link href="/">
    <a>hoge</a>
</Link>

buttonなどのインタラクティブコンポーネントを使って遷移したい場合は、next/routerを使いましょう。

import Router from 'next/router'

Router.push('/')

てな感じ本日も以上となります🍺

その他の記事

yamatooo.blog

yamatooo.blog

yamatooo.blog

【Git】別のブランチに特定のコミットを適用する

本日もGitの小ネタです👷‍♀️

別のブランチに特定のコミットを適用する

例えば、hogeブランチのコミットIDhoge123をmasterブランチに適用したい場合は、masterブランチにて下記を実行することでコミットを適用することができます。

$ git cherry-pick hoge123

てな感じで本日も以上となります🎣

その他の記事

yamatooo.blog

yamatooo.blog

yamatooo.blog

【Git】特手のコミットの差分を見てみましょう

本日はgit初心者向けの小ネタで、特定のコミットの差分を見る方法を紹介したいと思います👷‍♀️

結論

コミットIDを元に、git showコマンドを実行することで、コミットの差分を確認することができます。

$ git show commit_id

てな感じで本日も以上となります🐟

その他の記事

yamatooo.blog

yamatooo.blog

yamatooo.blog

【Swift】enumにnoneはなるべく使わない方がいいお話

本日もSwiftの小ネタになります👷‍♀️

enumにnoneを使う

特定のタイプに属さないタイプを表すとき下記のようにnoneという命名で、enumを定義することがあるかもしれませんが、このネームスペースはSwift.Optionalで定義してあるnoneと衝突する可能性があるので注意が必要です。

case TestType {
  case a
  case b
  case none
}

例えば下記のような場合、case .none == testとなるのはtestプロパティがnullの場合のみになります👀

let test: TestType? = TestType.none

if case .none == test {
  print("noneだよ")
}

てな感じで本日も以上となります🍺

その他の記事

yamatooo.blog

yamatooo.blog

yamatooo.blog

【RxSwift】Observableを再帰的に実行したい

Observableを再帰的に実行するサンプル実装をしたので、軽くメモしておきます👀 もっといい書き方あったらコメントで教えてください!

どんな状況?

やりたいこととしては、データIDの配列からページネーションしながらIDを取り出し、そのIDを元にデータを取得するという実装です。この時に問題となっているのが、データID配列の中に取得できない(取得しようとした際にエラーとなる)データが含まれていることです。つまり、エラーが発生した場合は、ページに含まれるデータの数が少なくなってしまうので、再帰的に次のIDからデータを取得してくる必要があります。

そしてできたのが、下記のような実装。再帰実行したいObservableをローカル関数として実装することで、再帰的なObservableを生成することができます。これにより、Errorが発生した場合でも、データを指定した数、再帰的に取得することができるようになります。

    private func get() -> Single<[Data]> {
        var currentItems: [Data] = []
        return Single.create {[unowned self] observer in
            @discardableResult
            func addDataIfNeed() -> Disposable {
                guard let id = currentData.popularProductIds[safe: pageData.currentIndex] else {
                    observer(.success(currentItems))
                    return Disposables.create()
                }
                return repository.get(id: id)
                    .catchErrorJustReturn(nil)
                    .subscribe(onSuccess: { data in
                        if (pageData.currentIndex - 1) >= pageData.ids.count || currentItems.count == perPage {
                            observer(.success(currentItems))
                        } else {
                            pageData.currentIndex += 1
                            if let data = data {
                                currentItems.append(data)
                            }
                            addDataIfNeed()
                        }
                    })
            }
            return addDataNeed()
        }
    }

@discardableResult属性については下記を参照してみてください。

yamatooo.blog

てな感じで本日も以上となります🍺

その他の記事

yamatooo.blog

yamatooo.blog

yamatooo.blog