我有来自json的数组:
var list = ["one"],["two"],["three"]
如何加入他们成为`[“一个”,“两个”,“三个”]
我正在使用此https://github.com/jriosdev/iOSDropDown
的下拉菜单它必须是:
//要显示的数组列表。可以动态更改。
trfBankTujuan.optionArray = ["nama 1", "nama 2", "nama 3", "nama 4"]
我想从JSON填充那些数组列表:
"data_transaction": [
{
"kode": "002",
"nama": "nama 1"
},
{
"kode": "011",
"nama": "nama 2"
},
{
"kode": "008",
"nama": "nama 3"
},
{
"kode": "009",
"nama": "nama 4"
}
]
var banknya: String = ""
class APITrf: NSObject {
class func BankList(completion: @escaping (_ error: Error?, _ banklikst: [Bank]?)->Void) {
Alamofire.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers)
.responseJSON { response in
switch response.result
{
case .failure(let error):
print(error)
case .success(let value):
let json = JSON(value)
print(json)
if let status = json["status"].string {
if (json["status"] == "00") {
print("status: \(status)" + " = sukses")
let dataBank = json["data_transaction"].array
}
} else {
if (json["status"] != "00") {
print("must return to login screen")
}
}
for (index,subJson):(String, JSON) in json["data_transaction"] {
if let nama_Bank = subJson["nama_bank"].string {
banknya = nama_Bank
print(banknya)
}
}
然后我要将那些对象“ banknya”放入:
import UIKit
import iOSDropDown
class AB: UIViewController {
func dropDownBankList() {
trfBankTujuan.optionArray = [banknya]
}
}
答案 0 :(得分:0)
考虑正确的list
您可以使用flatMap
var list = [["one"],["two"],["three"]]
var flatted = list.flatMap{ $0 } // result : ["one", "two", "three"]
答案 1 :(得分:0)
这是使用
的解决方案平面图
var theResult = list.flatMap {$0}
减少
var theResult = list.reduce([String](), { result, item in
return result + item.reduce([String](), { $0 + [$1] })
})
forEach <-最糟糕的解决方案
var theResult = [String]()
list.forEach { item in
item.forEach { subItem in
theResult.append(subItem)
}
}