如何获取存储在Swift中的NSMutableArray中的NSDictionary元素?

时间:2015-09-08 09:23:50

标签: ios swift nsmutablearray nsdictionary

我已通过此方法将联系人存储为可变数组上的字典:

 var addressBookReff: ABAddressBookRef = ABAddressBookCreateWithOptions(nil, nil).takeRetainedValue()
    var arrOfDictContacts:NSMutableArray = NSMutableArray()
    let people:NSArray = ABAddressBookCopyArrayOfAllPeople(addressBookReff).takeRetainedValue()
    for person in people{
        if  let name:String = ABRecordCopyValue(person, kABPersonFirstNameProperty)?.takeRetainedValue() as? String {
            let numbers:ABMultiValue = ABRecordCopyValue(person, kABPersonPhoneProperty).takeRetainedValue()
            if let number:String = ABMultiValueCopyValueAtIndex(numbers,0)?.takeRetainedValue() as? String {
                arrOfDictContacts.addObject(["\(name)":"\(number)"])
            }
        }
    }

这里,arrOfDictContacts是我的可变数组,其中包含名称和数字作为字典。像这样:

 arrOfDictContacts = ({ my = 12131;}, { doctor = 54445;}, { AL = 543212601;},  { customer = 121; } }

现在我有另一个名字数组

arrOfNames = [my, AL]

我想从arrOfNames

获取arrOfDictContacts的相应数字

期望输出:

  arrOfNumbers = [12131, 543212601]  

我该怎么做?

1 个答案:

答案 0 :(得分:0)

编辑:

由于您的数组包含字典,因此执行此操作的方法是针对您的数组检查每个字典。如下:

var arrOfNumbers : [String] = []
for dict in arrOfDictContacts {
    for name in arrOfNames {
        if let value = dict[name] as? String {
            arrOfNumbers.append(value)
        }
    }
}