ios - SWIFT 3 - take out html tags from string taken from JSON web url -
i wondering how can html tags stripped out of json web url. have use nsstring
of similar.
so looking strip out html tags in summary value. looked around abit , says nsstring can used not sure if implemented swift 3. appreciated.
my code:
import uikit import alamofire struct postinput { let mainimage : uiimage! let name : string! let author : string! let summary : string! } class tableviewcontroller: uitableviewcontroller { var postsinput = [postinput]() var mainurl = "https://www.example.com/api" typealias jsonstandard = [string : anyobject] override func viewdidload() { super.viewdidload() // additional setup after loading view, typically nib. callalamo(url: mainurl) } func callalamo(url : string){ alamofire.request(url).responsejson(completionhandler: { response in self.parsedata(jsondata: response.data!) }) } func parsedata(jsondata : data) { { var readablejson = try jsonserialization.jsonobject(with: jsondata, options: .mutablecontainers) as! jsonstandard // print(readablejson) if let posts = readablejson["posts"] as? [jsonstandard] { post in posts { let title = post["title"] as! string let author = post["author"] as! string guard let dic = post["summary"] as? [string: any], let summary = dic["value"] as? string else { return } print(author) if let imageurl = post["image"] as? string { let mainimageurl = url(string: imageurl ) let mainimagedata = nsdata(contentsof: mainimageurl!) let mainimage = uiimage(data: mainimagedata as! data) postsinput.append(postinput.init(mainimage: mainimage, name: title, author: author, summary: summary)) } } dispatchqueue.main.async { self.tableview.reloaddata() } } } catch { print(error) } } override func tableview(_ tableview: uitableview, numberofrowsinsection section: int) -> int { return postsinput.count } override func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> uitableviewcell { let cell = tableview.dequeuereusablecell(withidentifier: "cell") // cell?.textlabel?.text = titles[indexpath.row] let mainimageview = cell?.viewwithtag(2) as! uiimageview mainimageview.image = postsinput[indexpath.row].mainimage //(cell?.viewwithtag(2) as! uiimageview).image = postsinput[indexpath.row].mainimage let mainlabel = cell?.viewwithtag(1) as! uilabel mainlabel.text = postsinput[indexpath.row].name mainlabel.font = uifont(name: "helvetica", size:14) let autlabel = cell?.viewwithtag(3) as! uilabel autlabel.text = postsinput[indexpath.row].author autlabel.font = uifont(name: "helvetica", size:12) let sumlabel = cell?.viewwithtag(4) as! uilabel sumlabel.text = postsinput[indexpath.row].summary sumlabel.font = uifont(name: "helvetica", size:12) //(cell?.viewwithtag(3) as! uilabel).text = postsinput[indexpath.row].author return cell! } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } }
you can use code stripping html tags
from previous question
guard let dic = post["summary"] as? [string: any], let summary = dic["value"] as? string else { return } let str = summary.replacingoccurrences(of: "<[^>]+>", with: "", options: .regularexpression, range: nil) print(str)
edit
i have checked , working
let summary = "<p>latin text here</p>" let str = summary.replacingoccurrences(of: "<[^>]+>", with: "", options: .regularexpression, range: nil) print(str)
latin text here
Comments
Post a Comment