Swift에서 replace는 어떻게 하나 하면서 찾아본...

역시나 있다...


그것도 아주 이쁘게..


You can use this:

let s = "This is my string"
let modified = s.replace(" ", withString:"+")    

If you add this extension method anywhere in your code:

extension String
{
    func replace(target: String, withString: String) -> String
    {
       return self.stringByReplacingOccurrencesOfString(target, withString: withString, options: NSStringCompareOptions.LiteralSearch, range: nil)
    }
}

Swift 3:

extension String
{
    func replace(target: String, withString: String) -> String
    {
        return self.replacingOccurrences(of: target, with: withString, options: NSString.CompareOptions.literal, range: nil)
    }
}


실제 링크는 여기

http://stackoverflow.com/questions/24200888/any-way-to-replace-characters-on-swift-string


Swift2


Swift3



Kanna가 css을 지원한다는데

사실 문법을 잘 모르겠다 -_-;


그래서 좀 검색 해보니


튜토리얼이 있다.

http://tid-kijyun.github.io/Kanna/tutorials/v2/searching.html


그리고 구글을 통해 찾은 샘플 ? 이랄까...

이건 Alamofire랑 같이 해서 사용 하는 방법...

https://www.twilio.com/blog/2016/08/web-scraping-and-parsing-html-in-swift-with-kanna-and-alamofire.html


여기서 힌트를 얻고 하니 잘 된다...

문제가 되엇던 부분은...


이런식의 코드랄까 ? 특수기호를 위해선...저렇게....

for show in doc.css("td[id^='Text']") {

XCode를 7에서 8로 올리고 Use Legacy Swift 설정해도

Swift lib 문제가 있어서 검색을 하다가


Derived data를 지워보고라고 해서 찾아본


File > Workspace Settings > Project Settings 보면 Drived data로 갈 수 있는 링크(?)가 제공 !


http://stackoverflow.com/questions/38016143/how-to-delete-derived-data-in-xcode-8



swift 용 html parse



cocoapods

https://cocoapods.org/pods/Kanna


github

https://github.com/tid-kijyun/Kanna


github release

https://github.com/tid-kijyun/Kanna/releases


swift 2.x 버전에서 사용하기 위해서는 

1.1.1 까지 사용 !


iOS 개발 시 Label에 색을 넣을 때...한정적 색만 존재하여 여러가지 색상을 넣기 애매


RGB 색상을 넣기 위하여 구글 검색 시작 !


Swift 1.0을 1.2를 거쳐 2.0에 맞는 코드 찾음.


관련 질문을 stackoverflow에서 찾고

http://stackoverflow.com/questions/24074257/how-to-use-uicolorfromrgb-value-in-swift

댓글에서 아래 github를 찾고

https://gist.github.com/arshad/de147c42d7b3063ef7bc

github에서 댓글로 2.0에 맞는 코드를 발견 ㅎ


결국 만들어 낸 코드 !

extension UIColor {
    convenience init(hexString: String) {
        let hex = hexString.stringByTrimmingCharactersInSet(NSCharacterSet.alphanumericCharacterSet().invertedSet)
        var int = UInt32()
        NSScanner(string: hex).scanHexInt(&int)
        let a, r, g, b: UInt32
        switch hex.characters.count {
        case 3: // RGB (12-bit)
            (a, r, g, b) = (255, (int >> 8) * 17, (int >> 4 & 0xF) * 17, (int & 0xF) * 17)
        case 6: // RGB (24-bit)
            (a, r, g, b) = (255, int >> 16, int >> 8 & 0xFF, int & 0xFF)
        case 8: // ARGB (32-bit)
            (a, r, g, b) = (int >> 24, int >> 16 & 0xFF, int >> 8 & 0xFF, int & 0xFF)
        default:
            (a, r, g, b) = (1, 1, 1, 0)
        }
        self.init(red: CGFloat(r) / 255, green: CGFloat(g) / 255, blue: CGFloat(b) / 255, alpha: CGFloat(a) / 255)
    }
}

코드 사용법은

cell.Label.textColor = UIColor(hexString : "A9A9A9")

RGB코드는 여기 참고 http://www.javascripter.net/faq/rgbtohex.htm


+ Recent posts