Swift 2.0: Could not find overload. Need Explanation

527 Views Asked by At

I have the following code in my program.

    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)
                }
            }
        }
    }
}

On the line:

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

I get the following error: -Could not find an overload for 'pathForResource' that accepts the supplied arguments

Can someone explain this error to me and maybe suggest a solution. I have been reading through various searches on Google but can't quite seem to get what I need.

Thanks in advance.

1

There are 1 best solutions below

5
On

NSURL(fileURLWithPath:) is now return a NSURL so just don't use if let here.

@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.

for the line: if var template = NSString(contentsOfURL: templateURL, encoding: NSUTF8StringEncoding)?

Its decleration is:

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

So it not returns optional NSString either, so you cannot use if let again. You have to use do-try-catch here.

Apple's document about this: https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ErrorHandling.html#//apple_ref/doc/uid/TP40014097-CH42-ID508

Also, you should use multiple unwrapping optional:

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

Your code is a pyramid of doom :)