Avatar
↓は OK なのに struct Bar {} struct Foo { func bar() -> Bar? { return Bar() } } extension Int { func foo() -> Foo? { return Foo() } } let x = 42 let bar: Bar? = x.foo()?.bar() // ✅ OK
3:49 AM
↓はエラーなのは変だと思う。 struct Bar {} struct Foo { func bar() -> Bar? { return Bar() } } extension Int { func foo() throws -> Foo { return Foo() } } let x = 42 let bar: Bar? = try? x.foo().bar() // ⛔ NG
3:50 AM
↓こうしろということかもしれないけど。 struct Bar {} struct Foo { func bar() -> Bar? { return Bar() } } extension Int { func foo() throws -> Foo { return Foo() } } let x = 42 let bar: Bar? = (try? x.foo())?.bar() // ✅ OK (edited)