swift - 第一次使用 Xcode 7 时,我收到了一个额外的参数错误调用,代码如下 -

标签 swift

获取与下面的“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/

相关文章:

swift - 如何刷新 TableView 以使索引也更新?

ios - 如何快速增加 TabBar 项目标题的底部偏移量

ios - 可以不占领屏幕吗? Google 地方信息 IOS swift

objective-c - Swift 长按结果

ios - 属性初始值设定项在 'self' 可用错误 Swift 之前运行

ios - 提取 NSDate() 的值

uitableview - UITableViewCell 中具有动态高度的多个 UILabels

ios - 火力基地 |远程通知不显示,swift 3.0

ios - "defer"是否保证在 Swift 中被调用?

ios - 使用xib文件的自定义表格 View 的单元格,无法单击/选择/点击