나의 발자취
API 결과값 안에 tag가 있을 때 - Regex 본문
간혹가다 API 결과값에 태그가 있는 경우가 있다. 그럴 때는 Regex로 없애주면 되는데, 문제점은 제목 구분자로 <제목> 이런 식으로 쓴 것들은 다 날아간다.
extension String {
var stripHTML: String {
return self.replacingOccurrences(of: "<[^>]+>", with: "", options: .regularExpression)
}
func stripOutHtml() -> String? {
do {
guard let data = self.data(using: .unicode) else {
return nil
}
let attributed = try NSAttributedString(data: data, options: [.documentType:NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil)
return attributed.string
} catch {
return nil
}
}
}
'앱 개발 > iOS' 카테고리의 다른 글
iOS 무한 스크롤 구현하는 법: How to implement an Infinite Scrolling List (2) | 2024.09.16 |
---|---|
XCode에서 마크다운 형식 보는법 - 단축키 한번에 (0) | 2024.09.11 |
KakaoAPI를 활용한 BookSearch 앱 만들기 (2) (0) | 2024.09.09 |
KakaoAPI를 활용한 BookSearch 앱 만들기 (1) (3) | 2024.09.06 |
WebView: alert 만들기 (파일명: WebNativeCom) (9) | 2024.09.05 |
Comments