Swift 为什么一个表达模棱两可,另一个不模棱两可?
下面的代码可以工作Swift 为什么一个表达模棱两可,另一个不模棱两可?,swift,reduce,ambiguous,Swift,Reduce,Ambiguous,下面的代码可以工作 let evens = [1,2,2,3,4,5,6,6,7].reduce(Set<Int>()) { (var set: Set<Int>, int: Int) -> Set<Int> in if (true) { set.insert(int) } } let evens=[1,2,2,3,4,5,6,6,7].reduce(Set()){(var Set:Set
let evens = [1,2,2,3,4,5,6,6,7].reduce(Set<Int>()) { (var set: Set<Int>, int: Int) -> Set<Int> in
if (true) {
set.insert(int)
}
}
let evens=[1,2,2,3,4,5,6,6,7].reduce(Set()){(var Set:Set,int:int)->
如果(真){
set.insert(int)
}
}
…但编译器认为这是不明确的
let evens = [1,2,2,3,4,5,6,6,7].reduce(Set<Int>()) { (var set: Set<Int>, int: Int) -> Set<Int> in
set.insert(int)
}
let evens=[1,2,2,3,4,5,6,6,7].reduce(Set()){(var Set:Set,int:int)->
set.insert(int)
}
错误报告
下面的代码可以工作
let evens = [1,2,2,3,4,5,6,6,7].reduce(Set<Int>()) { (var set: Set<Int>, int: Int) -> Set<Int> in
if (true) {
set.insert(int)
}
}
不,它没有:
:; swift
"crashlog" and "save_crashlog" command installed, use the "--help" option for detailed help
"malloc_info", "ptr_refs", "cstr_refs", and "objc_refs" commands have been installed, use the "--help" options on these commands for detailed help.
Welcome to Apple Swift version 2.1 (700.1.101.6 700.1.76). Type :help for assistance.
1> let evens = [1,2,2,3,4,5,6,6,7].reduce(Set<Int>()) { (var set: Set<Int>, int: Int) -> Set<Int> in
2.
3. if (true) {
4.
5. set.insert(int)
6.
7. }
8.
9. }
evens: Set<Int> = {}
repl.swift:9:1: error: missing return in a closure expected to return 'Set<Int>'
}
^
:;敏捷的
如果安装了“crashlog”和“save_crashlog”命令,请使用“-help”选项获取详细帮助
已安装“malloc_info”、“ptr_refs”、“cstr_refs”和“objc_refs”命令,请使用这些命令上的“-help”选项获取详细帮助。
欢迎使用Apple Swift 2.1版(700.1.101.6 700.1.76)。类型:帮助寻求帮助。
1> 设evens=[1,2,2,3,4,5,6,6,7].reduce(Set()){(var-Set:Set,int:int)->Set-in
2.
3.如果(真){
4.
5.设置。插入(int)
6.
7. }
8.
9. }
evens:Set={}
回复:swift:9:1:错误:预期返回“Set”的闭包中缺少返回
}
^
在这两种情况下,问题是您没有返回任何内容,但需要返回类型为Set
:
let evens=[1,2,2,3,4,5,6,6,7].reduce(Set()){(var Set:Set,int:int)->
如果(真){
set.insert(int)
}
返回集
}
let evens=[1,2,2,3,4,5,6,6,7].reduce(Set()){(var Set:Set,int:int)->
set.insert(int)
返回集
}
对不起,我所说的“有效”是指我没有收到那个模棱两可的错误。真不敢相信我忘了回来……谢谢
let evens = [1,2,2,3,4,5,6,6,7].reduce(Set<Int>()) { (var set: Set<Int>, int: Int) -> Set<Int> in
set.insert(int)
return set
}