gpt4 book ai didi

xcode - Swift 2.0 : Could not find overload. 需要解释

转载 作者:行者123 更新时间:2023-11-30 10:14:27 26 4
gpt4 key购买 nike

我的程序中有以下代码。

    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 上进行各种搜索,但似乎不太能找到我需要的内容。

提前致谢。

最佳答案

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 { ... }

你的代码是一个末日金字塔:)

关于xcode - Swift 2.0 : Could not find overload. 需要解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31017549/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com