如何解析数组以返回集合?

时间:2019-06-04 08:38:06

标签: arrays swift dictionary

我有一个结构形式的特定模型:

struct ContactsModel {
  let name: String
  let status: String
  let number: String
  let onlineStatus: Bool
}

有一种方法可以解析名称数组并将其添加到新集合中。

    var contactsDictionary = [String: [String]]()
    var contactNameSectionTitles = [String]()
    var names = [String]()
    var contactsArray = [ContactsModel(name: "Test", status: "Test", number: "+7 999 999 99 99", onlineStatus: true)]

    func configurateDictionary() {
        names = contactsArray.map {$0.name}

        for value in names {
            let nameKey = String(value.prefix(1))

            if var namePrefix = contactsDictionary[nameKey] {
                namePrefix.append(value)
                contactsDictionary[nameKey] = namePrefix
            } else {
                contactsDictionary[nameKey] = [value]
            }
        }

        contactNameSectionTitles = [String](contactsDictionary.keys)
        contactNameSectionTitles = contactNameSectionTitles.sorted(by: { $0 < $1 })
    }

如何在方法中执行此操作,以使其不返回字符串,而是返回模型?我希望收集类型为

var contactsDictionary = [String: [ContactsModel]]()

4 个答案:

答案 0 :(得分:1)

您需要按进行字典分组,例如按名称do对数组进行分组

let arr = [ContactsModel]
let res = Dictionary(grouping: arr, by: { $0.name }) // [String:[ContactsModel]]

https://developer.apple.com/documentation/swift/dictionary/3127163-init

答案 1 :(得分:1)

有一个API可以将数组分组为字典

swaption_black_model = ql.Swaption(swap, ql.EuropeanExercise(swap.startDate()))
initial_vol_guess = 0.60


def find_implied_black(vol):
    black_vol = ql.SimpleQuote(vol)
    swaption_black_model.setPricingEngine(
    ql.BlackSwaptionEngine(ql.YieldTermStructureHandle(spot_curve), 
    ql.QuoteHandle(black_vol)))
    swaption_black_model_value = swaption_black_model.NPV()
    diff = swaption_normal_model_value - swaption_black_model_value

    return diff


implied_black_vol = optimize.newton(find_implied_black, initial_vol_guess)
implied_black_vol = ql.SimpleQuote(implied_black_vol)
swaption_black_model.setPricingEngine(
ql.BlackSwaptionEngine(ql.YieldTermStructureHandle(spot_curve), 
ql.QuoteHandle(implied_black_vol)))
swaption_black_model_value = swaption_black_model.NPV()

print('Normal swaption price is {}'.format(swaption_normal_model_value))
print('Black swaption price is {}'.format(swaption_black_model_value))

答案 2 :(得分:1)

此方法将返回[String: ContactsModel]的字典

func configurateDictionary() -> [String: ContactsModel] {
    let result = contactsArray.reduce( [String: ContactsModel](), { (d, e) -> [String: ContactsModel] in 
        var dict = d
        dict[e.name] = e 
        return dict
    })        
    return result
}

答案 3 :(得分:1)

如果您想更改configurateDictionary方法以返回[String: [ContactsModel]],请在函数内部创建一个临时字典,并在函数末尾将其返回

class ViewController: UIViewController {
    var contactsDictionary = [String: [ContactsModel]]()
    var contactNameSectionTitles = [String]()
    var names = [String]()
    var contactsArray = [ContactsModel(name: "Test", status: "Test", number: "+7 999 999 99 99", onlineStatus: true)]
    override func viewDidLoad() {
        super.viewDidLoad()
        contactsDictionary = configurateDictionary()
    }
    func configurateDictionary() -> [String: [ContactsModel]] {
        names.removeAll()
        contactNameSectionTitles.removeAll()
        var temp = [String: [ContactsModel]]()
        for contact in contactsArray {
            names.append(contact.name)
            let nameKey = String(contact.name.prefix(1))
            temp[nameKey, default: []].append(contact)
        }
        contactNameSectionTitles = [String](contactsDictionary.keys).sorted()
        return temp
    }
}