0

In this project I am trying to display a section of a webpage (weather-forecast.com) in my app. I've downloaded the html content into 'webContent' and used componentsSeparatedByString to break up the html code into an array of Strings to then grab the index I desire for my app. This part is in 'websiteArray1' and this value is a string "Mostly dry. Very mild (max 17°C on Sat afternoon, min 8°C on Fri night). Winds increasing (calm on Fri night, fresh winds from the ESE by Sun night)." It contains &deg instead of the symbol ° and I am trying to use the stringByReplacingOccurrencesOfString to replace &deg with °. I'm getting an that this method can't be called on a string but only NSString so then I tried converting my string to an NSString but now I am getting the error " [String] is not convertible to NSString".

Can anyone point me in the right direction on how to convert to an array of NSString or an alternative way to use the stringByReplacingOccurrenceOfString method?

let url = NSURL(string: "http://www.weather-forecast.com/locations/London/forecasts/latest")!

        let task = NSURLSession.sharedSession().dataTaskWithURL(url) { (data, response, error) -> Void in

            if let urlContent = data {

                let webContent = NSString(data: urlContent, encoding: NSUTF8StringEncoding)

                let websiteArray = webContent?.componentsSeparatedByString("3 Day Weather Forecast Summary:</b><span class=\"read-more-small\"><span class=\"read-more-content\"> <span class=\"phrase\">")



                if websiteArray?.count > 0{
                    //print(websiteArray![1])
                    var weatherArrayWithDeg = websiteArray![1].componentsSeparatedByString("</span>")

                    // Need to convert to NSString to use stringByReplacingOccurenceOfString, want to separate '$deg;' for ' °'
                    let weatherArray = (weatherArrayWithDeg as NSString).stringByReplacingOccurrencesOfString("&deg;", withString: " °")

                    print(weatherArrayWithDeg[0])
                }

            }
        }

        task.resume()

enter image description here

Talcicio
  • 65
  • 7

1 Answers1

0

I'll opt to solve your problem as opposed to your question:

Add as extension:

extension String {

    func convertFromHTML() -> String? {

        if let htmldata = self.data(using: String.Encoding.utf8), let attributedString = try? NSAttributedString(data: htmldata, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil) {
            return attributedString.string
        }

        return nil
    }
}

Usage:

let weather = "its 70&deg; outside"
print("weather: \(weather.convertFromHTML())")

Note you can now solve your problem on any string, in any part of your app, for any HTML that needs converted. And that's cool.

Frankie
  • 11,508
  • 5
  • 53
  • 60
  • Ok I am trying your method cause it does sound really convenient to know. I'm getting an error saying my value type String does not have a member 'data'. Am I supposed to make a var data and set it equal to something? I thought for a second that I should use my 'weatherArray' string but that doesn't seem right. – Talcicio Oct 29 '16 at 16:29
  • First see if you can get my `"its 70° outside"` example code working to make sure you've implemented the extension and usage correctly. If you get the example to work then edit your question with a screen shot of the error and I'll have a look. – Frankie Oct 29 '16 at 16:36
  • I went ahead and tried the example code you provided and I'm getting the same error about the 'self.data' variable. I edited my question with a screenshot of the playground implemented it in – Talcicio Oct 29 '16 at 17:40
  • I can see you're not using Swift 3.0. My example is in Swift 3.0 syntax. I think yours will be something like self.dataUsingEncoding(NSUTF8StringEncoding). There will likely be other syntax errors. You'll just have to make a few changes to get it in the right version of Swift for you. – Frankie Oct 29 '16 at 18:00