2016-04-20 13 views
0

をスローすることが無名関数を指定してください私のコードからストリップダウンサンプルです:スウィフト2.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 way to do the map? 
     widgets = try strings.map({(string:String) throws -> Widget in 
      return try Widget(string) 
     }) 
    } 
} 
+0

Btwをスローします。私はXCode 7.3を使用しています –

答えて

4

.maprethrowsキーワードでマークはそうすることができますだけ

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

以来Widget.initがも

+0

テスト済みです! –

関連する問題