Avatar
よし、ここまで書けた import Foundation public func recover <R> (_ f: () throws -> R, ifError: (Error) -> Void) -> Optional<R> { do { return try f() } catch let error { ifError(error) return nil } } struct Test { let url: URL enum InitError: Error { case unknown } init(urlString: String) throws { guard let url = URL(string: urlString) else { throw InitError.unknown } self.url = url } } func test(_ string: String) { guard let test: Test = recover({ return try Test(urlString: string) }, ifError: { print($0) }) else { return } print(type(of: test)) } test("http://www.google.com") // Test test("") // unknown