LoginSignup
6
4

More than 1 year has passed since last update.

[Swift] Computed Propertyにasyncとthrowsをつける方法

Last updated at Posted at 2021-11-03

モチベーション

functionでエラーを返す可能性がある時はthrowsをつけます。
Computed Propertyでもできそうだけとあんまりthrows使ってるの見ないなあと思ったのでどうやって書くのか調べてみました。
あとSwiftにasync-awaitが追加されたので、async-awaitと組み合わせるとどんな書き方になるか調べてみました。

環境

Swift5.5
Xcode13.1

ComputedPropertyにthrowsをつける書き方

以下のようにgetterの後にthrowsをつけます。

Before

var body: Data? {
    try? JSONEncoder().encode(parameters)
}

After

var body: Data {
    get throws {
        try JSONEncoder().encode(parameters)
    }
}

※ setにも同じようにつけてみましたが怒られました。

'set' accessor cannot have specifier 'throws' と出るのでthrowsを付けられるのはgetterだけっぽいですね。

var foo: String {
    get {
        ""
    }
    set throws { // `'set' accessor cannot have specifier 'throws'`

    }
}

ProtocolでthrowsつきのComputed Propertyを宣言することもできました。

Before

protocol RequestProtocol {
    var body: Data? { get }
}

After

protocol RequestProtocol {
    var body: Data { get throws }
}

ComputedPropertyにasyncとthrowsをつける書き方

以下のようにgetterの後にasyncとthrowsをつけるだけです。

var body: Data {
    get async throws {
        try await foo()
    }
}

func foo() async throws -> Data {
    Data()
}

呼び出す側

urlRequest.httpBody = try await requestItem.body

Protocolでの宣言時は以下のようになります。

protocol RequestProtocol {
    var body: Data { get async throws }
}

所感

async-awaitの登場で今までコールバックを渡していたfunctionがComputed Propertyに書き換わっていきそうな気がしています。
そうなるとthrowsやasyncなComputed Propertyを宣言する機会も増えてきそうなので、いい感じに予習ができてよかったです。

上記のコードはコンパイルが通るレベルでしか動作を確認できていないので、もし間違いがあればご指摘いただけると幸いです。

6
4
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
6
4