【问题标题】:Swift 2.0: Could not find overload. Need ExplanationSwift 2.0:找不到重载。需要说明
【发布时间】:2015-06-24 04:19:51
【问题描述】:

我的程序中有以下代码。

    var detailItem: RSSItem? {
    didSet {
        self.configureView()
    }
}

func configureView() {

    if let item: RSSItem = self.detailItem
    {
        if let webView = self.itemWebView
        {

            if let templateURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("template", ofType: "html")!)?
            {
                if var template = NSString(contentsOfURL: templateURL, encoding: NSUTF8StringEncoding)?
                {
                    if let title = item.title
                    {
                        template = template.stringByReplacingOccurrencesOfString("###TITLE###", withString: title)
                    }

                    if let content = item.content
                    {
                        template = template.stringByReplacingOccurrencesOfString("###CONTENT###", withString: content)
                    }
                    else if let description = item.itemDescription
                    {
                        template = template.stringByReplacingOccurrencesOfString("###CONTENT###", withString: description)
                    }

                    if let date = item.pubDate
                    {
                        var formatter = NSDateFormatter()
                        formatter.dateFormat = "MMM dd, yyyy"

                        template = template.stringByReplacingOccurrencesOfString("###DATE###", withString: formatter.stringFromDate(date))
                    }

                    if let author = item.author
                    {
                        template = template.stringByReplacingOccurrencesOfString("###AUTHOR###", withString: author)
                    }

                    webView.loadHTMLString(template, baseURL: nil)
                }

            }
            else
            {
                if let content = item.content
                {
                    webView.loadHTMLString(content, baseURL: nil)
                }
                else if let description = item.itemDescription
                {
                    webView.loadHTMLString(description, baseURL: nil)
                }
            }
        }
    }
}

上线:

if let templateURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("template", ofType: "html")!)?

我收到以下错误: - 找不到接受提供的参数的“pathForResource”的重载

有人可以向我解释这个错误,也许可以提出解决方案。我一直在阅读 Google 上的各种搜索,但似乎无法完全找到我需要的内容。

提前致谢。

【问题讨论】:

    标签: xcode swift nsurl xcode7 swift2


    【解决方案1】:

    NSURL(fileURLWithPath:) 现在返回NSURL,所以不要在这里使用if let

    @available(iOS 2.0, *)
    init(fileURLWithPath path: String, isDirectory isDir: Bool)
    init(fileURLWithPath path: String) // Better to use initFileURLWithPath:isDirectory: if you know if the path is a directory vs non-directory, as it saves an i/o.
    
    
    @available(iOS 2.0, *)
    class func fileURLWithPath(path: String, isDirectory isDir: Bool) -> NSURL
    class func fileURLWithPath(path: String) -> NSURL // Better to use fileURLWithPath:isDirectory: if you know if the path is a directory vs non-directory, as it saves an i/o.
    

    换行:if var template = NSString(contentsOfURL: templateURL, encoding: NSUTF8StringEncoding)?

    它的声明是:

    convenience init(contentsOfURL url: NSURL, encoding enc: UInt) throws
    convenience init(contentsOfFile path: String, encoding enc: UInt) throws
    

    所以它也不返回可选的NSString,所以你不能再次使用if let。你必须在这里使用do-try-catch

    Apple 关于此的文档: https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ErrorHandling.html#//apple_ref/doc/uid/TP40014097-CH42-ID508

    另外,你应该使用多个展开可选:

    if let x = OptionalX, y = OptionalY { ... }

    您的代码是pyramid of doom :)

    【讨论】:

    • 这很有帮助,但如何在我的代码中实现它?这也没有帮助我理解“过载”是什么意思
    • let templateURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("template", ofType: "html")!) 并删除您的 else 块。我也不明白“过载”是什么意思。 Swift 没有给我们提供太多关于这方面的细节。
    • 我明白了,谢谢你的帮助,让我实现这个。
    • 恐怕这行不通,在开始 let templateURL - 函数产生预期类型 'Bool' 的行后出现以下错误;你的意思是用'()'来调用它吗?
    • 这行代码也会出现同样的错误:if var template = NSString(contentsOfURL: templateURL, encoding: NSUTF8StringEncoding)?。我会在我的回答中更新它的声明。
    猜你喜欢
    • 1970-01-01
    • 2014-10-08
    • 1970-01-01
    • 2012-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-14
    相关资源
    最近更新 更多