我正在向UIWebView添加一些HTML内容。
这一行:
generatedHtml += "<br><p style=\"font-family:'Chevin-Medium';font-size:12px;color:#505050;padding-top:0px;\">" + newsItem.entry.likes + " like this " + newsItem.entry.comments?.count + " comments</p>"
我明白了:
expressions was too complex to be solved in reasonable time
我只是在计算一个阵列,我不知道如何让它变得那么复杂?
该对象如下所示:
public class NewsItem: NSObject {
var entry: EntryObject = EntryObject()
}
public class EntryObject: NSObject {
var comments: [Comment]? = []
}
答案 0 :(得分:2)
newsItem.entry.comments?.count
是一个整数,你不能使用+
向字符串添加整数,你应该使用\()
字符串插值:
" like this \(newsItem.entry.comments?.count) comments</p>"
如果您需要继续使用String
:
+
初始值设定项
" like this " + String(newsItem.entry.comments?.count) + " comments</p>"
如果错误&#34;太复杂&#34;坚持下去,你必须分解语句并使用变量而不是直接插入表达式。
答案 1 :(得分:1)
尝试这样做
var countComments : Int = 0
//Validate comment counting
if let cComments = newsItem.entry.comments?.count
{
countComments = cComments
}
//... Some code here ...
//Devide to Conquest.
//If is easy to find... Is not hard to fix
generatedHtml += "<br>"
generatedHtml += "<p style=\"font-family:'Chevin-Medium';font-size:12px;color:#505050;padding-top:0px;\">"
generatedHtml += "\(newsItem.entry.likes) "
generatedHtml += "like this \(countComments) comments" //Here you have a valid value
genetatedHtml += "</p>"
但是,为什么?
也许您对可选值 newsItem.entry.comments?.count 有疑问,可以获得零值。然后,首先验证值并确定返回的内容。更好“0”,有效值 nil
分割字符串创建时,调试工作将更容易执行。您可以更好地了解发生错误的位置。
也许这不是解决问题的最终解决方案,但也是帮助您解决问题的好方法。