Change a NSURL's scheme

前端 未结 6 2019
無奈伤痛
無奈伤痛 2021-02-02 09:34

Is there an easy way to change the scheme of a NSURL? I do realize that NSURL is immutable. My goal is to change the scheme of an URL to \"https\" if t

6条回答
  •  佛祖请我去吃肉
    2021-02-02 10:33

    Swift5

    extension URL {
        func settingScheme(_ value: String) -> URL {
        let components = NSURLComponents.init(url: self, resolvingAgainstBaseURL: true)
        components?.scheme = value
        return (components?.url!)!
    }
    

    }

    Usage

    if nil == url.scheme { url = url.settingScheme("file") }
    

提交回复
热议问题