0

这是我的代码中的一个精简示例:

struct Widget {
    let string: String
    init(_ string: String) throws {
        self.string = string
    }
}

struct Widgets {
    let widgets: [Widget]

    init(_ strings: [String]) throws {
        // Is this really the cleanest way to do the map?
        widgets = try strings.map({(string:String) throws -> Widget in
            return try Widget(string)
        })
    }
}
4

1 回答 1

4

.map标有rethrows关键字,所以你可以

init(_ strings: [String]) throws {
    widgets = try strings.map(Widget.init)
}

因为Widget.initthrows.map也会抛出

于 2016-04-20T11:09:46.363 回答