以下代码生成Computed property must have an explicit type error.
使用新的try/catch
Swift语法列出文件的正确方法是什么?
do {
let files = try NSFileManager.defaultManager().contentsOfDirectoryAtPath(docsPath as String) as! [String] {
print("Found \(files.count) file(s) in \(docsPath):")
}catch{
print("Error with listing files: \(error)")
}
答案 0 :(得分:3)
您在不需要的try语句({
之后)之后添加了一个额外的大括号... as! [String]
。这个额外的大括号让Swift相信你正在使用计算属性。
删除括号,do-try-catch
块应该有效:
var docsPath : String = "notavalidpath"
do {
let files = try NSFileManager.defaultManager().contentsOfDirectoryAtPath(docsPath as String) as! [String]
print("Found \(files.count) file(s) in \(docsPath):")
}catch{
print("Error with listing files: \(error)")
}