한글 인코딩때문에 2시간이나 헤맸다.


keycharsexample
urlFragmentAllowed!$&'()*+,-./:;=?@_~http://google.com?a=b&c=d
urlHostAllowed!$&'()*+,-.:;=[]_~http:%2F%2Fgoogle.com%3Fa=b&c=d
urlPathAllowed!$&'()*+,-./:=@_~http://google.com%3Fa=b&c=d
urlQueryAllowed!$&'()*+,-./:;=?@_~http://google.com?a=b&c=d
urlUserAllowed!$&'()*+,-.;=_~http%3A%2F%2Fgoogle.com%3Fa=b&c=d
urlPathAllowed!$&'()*+,-./:=@_~http://google.com%3Fa=b&c=d


String 변수에를 

let stringAll =  "자신의 URL 주소"

let str_url = stringAll.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)

이렇게 하면 파라미터의 값에 한글이 들어가도 값이 nil이 아니게 된다.


다른 예제로

func cs(cs:CharacterSet)->String {
    var s = ""

    let ar = [0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,
              0x3a,0x3b,0x3c,0x3d,0x3e, 0x3f,
              0x40,
              0x5b,0x5c,0x5d,0x5e,0x5f,
              0x7b,0x7c,0x7d,0x7e,0x7f,
              ]

    for i in ar {
        let uc = UnicodeScalar(i)!
        if cs.contains(uc) {
            s += String(uc)
        }
    }

    let url = "http://google.com?a=b&c=d"
    s += "   " +  url.addingPercentEncoding(withAllowedCharacters: cs)!

    return s

}


+ Recent posts