swift 3 从查询参数中获取子字符串 [英] swift 3 get a substring from query params

查看:41
本文介绍了swift 3 从查询参数中获取子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 swift 3 中从更大的字符串中获取一个子字符串.该字符串只是 url 的查询部分:

user_one_id=7&user_two_id=5

我只想要每个用户的 id,所以我想要 7 和 5.

解决方案

一个非常简单的解决方案是创建 URLComponents,分配查询部分并将查询项(平面)映射到它们的值

let string = "user_one_id=7&user_two_id=5"let components = URLComponents(string: "http://dummy.com?" + string)让 userIDs = components?.queryItems?.flatMap{ $0.value }打印(用户 ID)

let string = "user_one_id=7&user_two_id=5"var 组件 = URLComponents()components.query = 字符串让 userIDs = components.queryItems!.flatMap{ $0.value }打印(用户 ID)

I want to get a substring from a larger string in swift 3. The string is just the query part of a url:

user_one_id=7&user_two_id=5

I just want the id of each user, so I want 7 and 5.

解决方案

A quite simple solution is to create URLComponents, assign the query part and (flat)map the query items to their values

let string = "user_one_id=7&user_two_id=5" let components = URLComponents(string: "http://dummy.com?" + string) let userIDs = components?.queryItems?.flatMap{ $0.value } print(userIDs)

let string = "user_one_id=7&user_two_id=5"
var components = URLComponents()
components.query = string
let userIDs = components.queryItems!.flatMap{ $0.value }
print(userIDs)

这篇关于swift 3 从查询参数中获取子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆