0

I am attempting to get the index value of an array of CNLabelValue phone labels. I could iterate over them and get it that way but I know the .idex method would work just can't figure the syntax after of:

let labelIndex = mutableContact.phoneNumbers.index(of: <#T##CNLabeledValue<CNPhoneNumber>#>)
Jeremy Andrews
  • 807
  • 12
  • 17

3 Answers3

0

May be you are looking for this:-

let arr = ["a","b","c"]
if let index = arr.index(where: { (item) -> Bool in
  return (item == "c") 
}) {
    print(index)
}

Output: 2

Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56
  • Thanks I however tried this with CNLabeledValue array and get an error: PhoneLabelsTableViewController.swift:281:18: Binary operator '==' cannot be applied to operands of type 'CNLabeledValue' and 'String' So it comes down to a type issue – Jeremy Andrews Aug 07 '17 at 05:47
  • You need to conform equatable protocol for this. – Hussain Shabbir Aug 07 '17 at 05:49
0

You should do something like this:

let idx = mutableContact.phoneNumbers.index(where: { (model) -> Bool in

        return if model == YOUR_OBJ

})

here,

(model) is of type CNPhoneNumber

and in the if check you should put the check for which you want to get the index.

(model) should be compared with your own obj for which you want to filter.

Umar Farooque
  • 2,049
  • 21
  • 32
  • Thanks - will try this - I have used iterating and that worked but it is the long way round. – Jeremy Andrews Aug 06 '17 at 16:30
  • Cool lemme know in case you need help. Try to avoid iteration though. – Umar Farooque Aug 06 '17 at 16:30
  • I am trying the code now however I don't understand what the (model) would be. I think the following might be the right approach I just don't understand the syntax: let idx = mutableContact.phoneNumbers.index(of: CNLabeledValue, "iPhone") Gives the following error: .... PhoneLabelsTableViewController.swift:280:42: Argument labels '(of:, _:)' do not match any available overloads. – Jeremy Andrews Aug 07 '17 at 05:42
  • model is already of the type that you are using, i.e. phoneNumbers, just put any name like model or obj to refer the index while the index(of:) function iterates through your collection – Umar Farooque Aug 07 '17 at 05:51
  • http://www.kaleidosblog.com/swift-data-structure-how-to-create-sort-filter-array-of-objects-in-swift should help. – Umar Farooque Aug 07 '17 at 06:15
  • Thanks for the help - can't get it quite right with either Umar or Hussain's answers so as a work-around I have used iteration - see my answer. Works fine but not the correct way. – Jeremy Andrews Aug 07 '17 at 06:54
0
var labelIndex = 0

for (i,bod) in mutableContact.phoneNumbers.enumerated() {
    print(i,bod.label!)
    if bod.label == labelP {labelIndex = i; break}      
}

mutableContact.phoneNumbers.remove(at: labelIndex)

A routine to iterate over all the CNLabelValues for .phonenumbers and then test if they match labelP (function parameter) and return the index (labelIndex = i) of a matched item.

then mutableContact.phoneNumbers.remove(at: labelIndex)`

Jeremy Andrews
  • 807
  • 12
  • 17