尝试在下面创建数组时,出现以下错误:
表达式的类型是不明确的,没有更多的上下文
在Swift中可以使用这样的数组吗?如果是的话,它是如何宣布的?
var jobArray = [
["Dog Walker", "Job Description", ["Monday", "Wednesday", "Friday"], "7"],
["Babysitter", "Job Description", ["Tuesday", "Wednesday"], "15"],
["Leaves Raker", "Job Description", ["Sunday", ""], "10"]
]
答案 0 :(得分:3)
我猜你在游乐场里没有导入Foundation
或类似的东西:
// uncommenting the below line fixes the problem
// import Foundation
// with it commented out,
var jobArray = [
["Dog Walker", "Job Description", ["Monday", "Wednesday", "Friday"], "7"],
["Babysitter", "Job Description", ["Tuesday", "Wednesday"], "15"],
["Leaves Raker", "Job Description", ["Sunday", ""], "10"]
]
没有它你会得到两个错误:
error: '_' is not convertible to 'StringLiteralConvertible' ["Dog Walker", "Job Description", ["Monday", "Wednesday", "Friday"], "7"], ^~~~~~~~~~~~~~~~~ error: type of expression is ambiguous without more context
第一个更为重要。第二个是斯威夫特上楼回家的神器。
为什么呢?因为[jobArray]
的类型是什么? Swift标准库中没有符合此定义的类型 - 您需要[[Something]]
Something
符合StringLiteralConvertible
("Dog Walker"
)和{{} ArrayLiteralConvertible
1}}(对于["Sunday", ""]
)。而且没有一个。
可悲的是,非常遗憾的是恕我直言,在<{1}}内定义 。这就是添加导入时编译的原因。
我强烈建议您不要像这样定义这个数组,而是实现一个简单的Foundation
:
struct