"Missing return in a closure expected to return 'SomeType'" .map 中的错误
"Missing return in a closure expected to return 'SomeType'" error in .map
我有以下代码:
struct AInt {
var aInt: Int
}
struct ADouble {
var aDouble: Double
static func convert(aInt: AInt) throws -> ADouble {
return ADouble(aDouble: Double(aInt.aInt))
}
}
struct B {
func doAction(aInts: [AInt]) throws -> [ADouble] {
return aInts.map { aInt in
do {
try ADouble.convert(aInt)
}
catch {
print(error)
}
}
// ^^^ error here: Missing return in a closure expected to return 'ADouble'
}
}
let aInts = [AInt(aInt: 2), AInt(aInt: 3)]
let b = B()
do {
print(try b.doAction(aInts))
}
catch {}
当我尝试使用可能 throw
错误的函数将 [AInt]
转换为 .map
中的 [ADouble]
时,我收到此错误:
Missing return in a closure expected to return 'ADouble'
.
好吧,我决定在 .map
的末尾添加 return
语句,如下所示:
return aInts.map { aInt in
do {
try ADouble.convert(aInt)
}
catch {
print(error)
}
return ADouble(aDouble: 2.2)
}
错误消失,但是当我在同一个 aInts
数组上打印 try b.doAction(aInts)
时,我得到这个:[ADouble(aDouble: 2.2), ADouble(aDouble: 2.2)]
,即它打印我手动设置的 ADouble(aDouble: 2.2)
。显然,这不是我想要的,所以我尝试在 try ADouble.convert(aInt)
之前添加 return
,如下所示:
return aInts.map { aInt in
do {
return try ADouble.convert(aInt)
}
catch {
print(error)
}
return ADouble(aDouble: 2.2)
}
现在我得到了正确的结果:[ADouble(aDouble: 2.0), ADouble(aDouble: 3.0)]
。但是如果 .map
末尾没有 return
语句,这段代码仍然无法工作。有什么想法可以摆脱它吗?
map()
方法声明为
@rethrows public func map<T>(@noescape transform: (Self.Generator.Element) throws -> T) rethrows -> [T]
这意味着(如果我理解正确的话)转换可以
return 一个值或抛出一个错误(这将是
转发给来电者)。
这会按预期编译和工作:
struct B {
func doAction(aInts: [AInt]) throws -> [ADouble] {
return try aInts.map { aInt in
return try ADouble.convert(aInt)
}
}
}
从 ADouble.convert(aInt)
抛出的错误将被转发到
map()
的调用者和从那里到 doAction()
.
的调用者
我有以下代码:
struct AInt {
var aInt: Int
}
struct ADouble {
var aDouble: Double
static func convert(aInt: AInt) throws -> ADouble {
return ADouble(aDouble: Double(aInt.aInt))
}
}
struct B {
func doAction(aInts: [AInt]) throws -> [ADouble] {
return aInts.map { aInt in
do {
try ADouble.convert(aInt)
}
catch {
print(error)
}
}
// ^^^ error here: Missing return in a closure expected to return 'ADouble'
}
}
let aInts = [AInt(aInt: 2), AInt(aInt: 3)]
let b = B()
do {
print(try b.doAction(aInts))
}
catch {}
当我尝试使用可能 throw
错误的函数将 [AInt]
转换为 .map
中的 [ADouble]
时,我收到此错误:
Missing return in a closure expected to return 'ADouble'
.
好吧,我决定在 .map
的末尾添加 return
语句,如下所示:
return aInts.map { aInt in
do {
try ADouble.convert(aInt)
}
catch {
print(error)
}
return ADouble(aDouble: 2.2)
}
错误消失,但是当我在同一个 aInts
数组上打印 try b.doAction(aInts)
时,我得到这个:[ADouble(aDouble: 2.2), ADouble(aDouble: 2.2)]
,即它打印我手动设置的 ADouble(aDouble: 2.2)
。显然,这不是我想要的,所以我尝试在 try ADouble.convert(aInt)
之前添加 return
,如下所示:
return aInts.map { aInt in
do {
return try ADouble.convert(aInt)
}
catch {
print(error)
}
return ADouble(aDouble: 2.2)
}
现在我得到了正确的结果:[ADouble(aDouble: 2.0), ADouble(aDouble: 3.0)]
。但是如果 .map
末尾没有 return
语句,这段代码仍然无法工作。有什么想法可以摆脱它吗?
map()
方法声明为
@rethrows public func map<T>(@noescape transform: (Self.Generator.Element) throws -> T) rethrows -> [T]
这意味着(如果我理解正确的话)转换可以 return 一个值或抛出一个错误(这将是 转发给来电者)。
这会按预期编译和工作:
struct B {
func doAction(aInts: [AInt]) throws -> [ADouble] {
return try aInts.map { aInt in
return try ADouble.convert(aInt)
}
}
}
从 ADouble.convert(aInt)
抛出的错误将被转发到
map()
的调用者和从那里到 doAction()
.