Swift 获取URL字符串中对应字段的vaule

2019-01-21  本文已影响7人  烟雨痕
    工具类或者String分类中实现
   //获取url中对应的参数的值
    static func paramValueOfUrl(url: String, with param: String) -> String?  {
        guard let url = URLComponents.init(string: url) else { return nil }
        return url.queryItems?.first(where: {$0.name == param})?.value
    }
使用:
if  let articleId: String = MSFHelper.paramValueOfUrl(url: link, with: "id") {
        MSFArticleDetailVM.requestArticleDelete(with: Int(articleId) ?? 0, successClosure: { (result) in
        NotificationCenter.default.post(name: NSNotification.Name.deleteArticle,
                                                    object: nil,
                                                    userInfo: ["success": true, "errMsg": ""])
                }) { (error) in
        NotificationCenter.default.post(name: NSNotification.Name.deleteArticle,
                                                    object: nil,
                                                    userInfo: ["success": false, "errMsg": error.errmsg])
                }
            }
上一篇下一篇

猜你喜欢

热点阅读