Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Swift 2.2中,如何简洁地指定可能引发_Swift_Lambda_Swift2.2 - Fatal编程技术网

在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