在Swift 2.2中,如何简洁地指定可能引发
下面是我代码中的一个精简示例:在Swift 2.2中,如何简洁地指定可能引发,swift,lambda,swift2.2,Swift,Lambda,Swift2.2,下面是我代码中的一个精简示例: 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
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)
})
}
}
.map
标记有rethrows
关键字,因此您可以
init(_ strings: [String]) throws {
widgets = try strings.map(Widget.init)
}
因为
Widget.init
抛出.map
也抛出.map
标记有重推
关键字,所以您可以
init(_ strings: [String]) throws {
widgets = try strings.map(Widget.init)
}
因为
Widget.init
抛出.map
也抛出Btw。我使用的是XCode 7.3Btw。我使用的是XCode 7.3