Swift   发布时间:2022-03-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了数组 – 在flatMap中“无法转换返回表达式”,其中包含无意义的表达式大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

我正在检查.lazy的高阶函数,并且得到了一些与flatMap函数相关的有趣编译错误(可能还有其他函数) 例子 let array = [1, 2, 3, 4, 5, 6] array .flatMap { print("DD") return $0 // CAnnot convert return expression of type 'Int' t
我正在检查.lazy的高阶函数,并且得到了一些与flatMap函数相关的有趣编译@L_696_2@(可能还有其他函数)

例子

 
let array = [1,2,3,4,5,6]

array
    .flatMap {
        print("DD")
        return $0 // CAnnot convert return expression of type 'Int' to return type 'String?'
    }
    .forEach {
        print("SS")
        print($0)
}

评论一下

 
array
    .flatMap {
//        print("DD")
        return $0
    }
    .forEach {
        print("SS")
        print($0)
}

一切正常……更有趣的例子

 
array
    .flatMap {
        let z = $0
        return $0  // Or return z - all is the same "CAnnot convert return expression of type 'Int' to return type 'String?'"
    }
    .forEach {
        print("SS")
        print($0)
}

什么可能导致这种行为?

@H_673_30@
目前Sequence上的flatMap(_ :)方法(从Swift 4开始)有两个不同的含义:

>它可以采用转换闭包返回一个可选的T ?,它将返回一个[T],过滤掉nil结果(在未来版本中将此过载is to be renamed转换为compactMap(_ :)).

public func flatMap<ElementOfResult>(
  _ transform: (Element) throws -> ElementOfResult?
) rethrows -> [ElementOfResult]

>它可以采用返回Sequence的转换闭包,它将返回一个包含所有结果序列的串联的数组.

public func flatMap<SegmentOfResult : Sequence>(
  _ transform: (Element) throws -> SegmentOfResult
) rethrows -> [SegmentOfResult.Element]

现在,在Swift 4中,String成为RangereplaceableCollection(因此也是一个SequencE).所以Swift 3代码做到了这一点:

// returns ["foo"],as using the `nil` filtering flatMap,the elements in the closure
// are implicitly promoted to optional Strings.
["foo"].flatMap { $0 }

在这样做:

// returns ["f","o","o"],a [Character],as using the Sequence concatenation flatMap,// as String is Now a Sequence (compiler favours this overload as it avoids the implicit
// conversion from String to String?)
["foo"].flatMap { $0 }

为了保持源兼容性,专门的flatMap为字符串重载were added

这样上面的用法仍会在Swift 3兼容模式下返回[String],而在Swift 4中返回[Character].

那么,为什么呢

let array = [1,6]

array
    .flatMap {
        print("DD")
        return $0 // CAnnot convert return expression of type 'Int' to return type 'String?'
    }
    .forEach {
        print("SS")
        print($0)
    }

告诉你闭包应该返回一个字符串?

好吧,Swift目前不会推断多语句闭包的参数和返回类型(更多信息请参见this Q&A).所以flatMap(_ :)重载了闭包返回泛型T的位置?或者通用S:序列没有显式类型注释就没有资格被调用,因为它们需要类型推断来满足通用占位符.

因此,唯一符合条件的重载是特殊的String源兼容性,因此编译器期望闭包返回一个String?.

解决此问题,您可以显式注释闭包的返回类型:

array
  .flatMap { i -> Int? in
    print("DD")
    return i
  }
  .forEach {
    print("SS")
    print($0)
  }

但是,如果您实际上没有在实际代码中使用此flatMap(_ :)重载的可选过滤功能,则应使用map(_ :)代替.

大佬总结

以上是大佬教程为你收集整理的数组 – 在flatMap中“无法转换返回表达式”,其中包含无意义的表达式全部内容,希望文章能够帮你解决数组 – 在flatMap中“无法转换返回表达式”,其中包含无意义的表达式所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。