LoginSignup
4
1

More than 5 years have passed since last update.

[Swift] イニシャライザをthrowsにする

Posted at

今知ったんですが、Swiftではイニシャライザもthrowsにすることができます。

struct Hoge {

    let hoge: Int

    init(hogeString: String) throws {

        guard let hoge = Int(hogeString) else {

            throw NSError(domain: "Hoge", code: 1, userInfo: nil)
        }

        self.hoge = hoge
    }
}
4
1
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
4
1