三套以下如何做一套? .我的意思是一个嵌套集
var nameSets : Set<String> = ["Nazmul","Hasan","Prince"]
var countrySets : Set<String> = ["UK","London","Bangladesh"]
var securitys : Set<Int> = [2904,99895,8944]
//work
var addresSets : Set = [nameSet,countrySet]
//cannot convert value of type '[Any]' to specified type 'Set'
var addresSets : Set = [nameSet,countrySet,security]
// type 'Any' does not conform to protocol 'Hashable'
var addresSets : Set<Any> = [nameSet,countrySet,security]
最佳答案
我不确定你想在这里实现什么,但是首先让我们清理你的代码
let names: Set = ["Nazrul", "Hasan", "Prince"]
let countries: Set = ["UK", "London", "Bangladesh"]
let securities: Set = [2904, 99895, 8944]
我们有 2 Set(s)
的 String
和 1 Set
的 Int
.
名字和国家
现在让我们创建一个包含names
的新集合。和 countries
.
let all: Set = [names, countries]
all
的推断类型是Set<Set<String>>
.
名称、国家和证券
让我们改变一下all
现在有人居住
let all: Set = [names, countries, securities]
error: argument type 'Set<_>' does not conform to expected type 'Any'
现在编译器需要一点帮助来弄清楚 all
应该定义。正确的定义是Set<Set<AnyHashable>>
所以
let all: Set<Set<AnyHashable>> = [names, countries, securities]
结果
现在all
包含 3 个原件 Set(s)
.
[
Set([AnyHashable(99895), AnyHashable(8944), AnyHashable(2904)]),
Set([AnyHashable("Nazmul"), AnyHashable("Prince"), AnyHashable("Hasan")]),
Set([AnyHashable("London"), AnyHashable("Bangladesh"), AnyHashable("UK")])
]
关于swift - 如何在swift中设置嵌套?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39645433/