我想从“ 标题”键中获取所有值的数组
ClientConfig config = new ClientConfig();
config.connectorProvider(new ApacheConnectorProvider());
config.property(ClientProperties.PROXY_URI, "proxy_url");
config.property(ClientProperties.PROXY_USERNAME,"user_name");
config.property(ClientProperties.PROXY_PASSWORD,"password");
Client client = ClientBuilder.newClient(config);
我想在选择器视图中使用此标题。
答案 0 :(得分:2)
您可以使用compactMap
:
let titleArr = arrSportsList.compactMap { $0["title"] }
它将每个字典转换为与键title
关联的值,并删除没有title
键的字典。
我还建议您创建一个类/结构来存储这些运动,而不要使用字典:
struct Sport {
let title: String
// other properties
}
答案 1 :(得分:1)
使用compactMap(_:)
方法从所有词典中获取标题值。如果任何词典不包含标题键,它将被忽略
var arrSportsList:[[String:String]] = []
// viewDidLoad code
arrSportsList = [
["title1":"anothergame"],
["title":"Cricket"],
["title":"Soccer"],
["title":"American Football"],
["title":"Ice Hockey"],
["title":"Tennis"],
["title":"Baseball"],
["title":"Basketball"],
]
let titleArr = arrSportsList.compactMap { $0["title"] }
print(titleArr)//Cricket,Soccer,American Football,Ice Hockey,Tennis,Baseball,Basketball
答案 2 :(得分:0)
最简单的方法
let titleArr = arrSportsList.compactMap { $0["title"] }
或者您可以遍历字典数组并获得像这样的标题
let titleArr = Array<String>();
for dict in arrSportsList {
if let title = dict["title"] {
titleArr.append(title);
}
}
答案 3 :(得分:0)
如果需要进一步了解,可以使用
// Create variable
var arrSportsList:[[String:String]] = []
// viewDidLoad code
arrSportsList = [
["title":"Cricket"],
["title":"Soccer"],
["title":"American Football"],
["title":"Ice Hockey"],
["title":"Tennis"],
["title":"Baseball"],
["title":"Basketball"],
]
var titleArray: [String]
for (key, value) in arrSportsList {
print (key) // "title"
print (value) //Cricket, Soccer, American Football, Ice Hockey, Tennis, Baseball, Basketball
titleArray.append(value)
}
print (titleArray) //Cricket, Soccer, American Football, Ice Hockey, Tennis, Baseball, Basketball