Best way to parse URL string to get values for keys?

前端 未结 16 1120
广开言路
广开言路 2020-11-29 18:36

I need to parse a URL string like this one:

&ad_eurl=http://www.youtube.com/video/4bL4FI1Gz6s&hl=it_IT&iv_logging_level=3&ad_flags=0&ends         


        
16条回答
  •  北荒
    北荒 (楼主)
    2020-11-29 19:14

    If you want to do the same thing in swift, you can use an extension.

    extension NSURL {
        func queryDictionary() -> [String:String] {
            let components = self.query?.componentsSeparatedByString("&")
            var dictionary = [String:String]()
    
            for pairs in components ?? [] {
                let pair = pairs.componentsSeparatedByString("=")
                if pair.count == 2 {
                    dictionary[pair[0]] = pair[1]
                }
            }
    
            return dictionary
        }
    }
    

提交回复
热议问题