简体   繁体   中英

Swift Adding HTML to UIWebView, expression was too complex

I'm adding some HTML content to an UIWebView.

This line:

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>"

I get:

expressions was too complex to be solved in reasonable time

I'm just doing a count on an array, i don't know how to make that less complex?

The object looks like this:

public class NewsItem: NSObject {
    var entry: EntryObject = EntryObject()

}

public class EntryObject: NSObject {
    var comments: [Comment]? = []
}

newsItem.entry.comments?.count is an integer, and you can't add an integer to a string using + , you should use string interpolation with \\() :

" like this \(newsItem.entry.comments?.count) comments</p>"

Or use the String initializer if you need to keep using + :

" like this " + String(newsItem.entry.comments?.count) + " comments</p>"

If the error "too complex" persists, you'll have to break down the statements and use variables instead of inserting the expressions directly.

Try to do by this way

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>"

But, why?

Maybe you have a problem with the optional value newsItem.entry.comments?.count that can gets you a nil value. Then, first of all, validate the value and be sure about what was returned. Better "0" , a valid value than nil

When you split the string creation, the debug working will be more easy to execute. You will can have a better idea where is happening an error.

Maybe it´s not a definitive solution to your problem, but a good way to help you fix it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM