获取与下面的“items”常量相关的额外参数错误调用。这在 Xcode 7.0 之后有变化吗?
override func viewDidLoad() {
super.viewDidLoad()
let fm = NSFileManager.defaultManager()
let path = NSBundle.mainBundle().resourcePath!
let items = fm.contentsOfDirectoryAtPath(path, error: nil)
for item in items as! [String] {
if item.hasPrefix("nssl") {
objects.append(item)
}
}
最佳答案
根据 Apple 的 contentsOfDirectoryAtPath
:
In Swift, this method returns a nonoptional result and is marked with the throws keyword to indicate that it throws an error in cases of failure.
You call this method in a try expression and handle any errors in the catch clauses of a do statement, as described in Error Handling in The Swift Programming Language (Swift 2).
在 Swift 2.0 中新的/正确的方法是这样的:
let fm = NSFileManager.defaultManager()
let path = NSBundle.mainBundle().resourcePath!
do {
let items = try fm.contentsOfDirectoryAtPath(path)
for item in items as! [String] {
if item.hasPrefix("nssl") {
objects.append(item)
}
}
}
catch let error as NSError {
error.description
}
关于swift - 第一次使用 Xcode 7 时,我收到了一个额外的参数错误调用,代码如下 -,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32655381/