0

I have checked this SO answer to find a solution for the deprecated find method, but the solution is for single array. In my project I am have two arguments that are both collection types. Through refactoring I am receiving the familiar conditional binding errors.

I have tried removing the conditional binding and using index(of: ) as in the SO answer I referenced, but since I'm not working with single elements like String types naturally I get tuple errors.

If methods can't be called on tuples why was the original find method able to call the tuple in the Swift 2 line below?

class MoneyPickerTableViewController: UITableViewController {   

var money: [String: String]
var purchaseOrder: [String]
var chosenKey: String = USDPreferences.shared().object(forKey: kUSDChosenMoneyKey) as! String    

// Swift 2
if let index = find(purchaseOrder, chosenKey) {
let indexPath = NSIndexPath(forRow: index, inSection: 0)

tableView.selectRowAtIndexPath(indexPath, animated: animated, scrollPosition: .Middle)
}    

navigationController?.setNavigationBarHidden(false, animated: animated)
}

Swift 3

let collections = (purchaseOrder, chosenKey) as ([String], String) 
let index = collections.index(of: )
Edison
  • 11,881
  • 5
  • 42
  • 50
  • You can't call methods on tuples and just expect that the method is applied to each tuple member. That's just not how Swift works – Alexander Aug 14 '17 at 22:58
  • Thanks. I realize that. That's why I said "naturally I'm getting these errors". I am looking for a way to index the two collections. The original code was indexing fine using `find(purchaseOrder, chosenKey)`. p.s the `find` method was fine with the tuple before so why not now? – Edison Aug 14 '17 at 23:00
  • that's not a tuple, it's a function call containing two arguments `purchaseOrder` and `chosenKey`. – Alexander Aug 14 '17 at 23:03
  • Ok I understand because of the `find` method it is a function call. And now there is no function call so it is a tuple. I have to make it a function call again using `index(of:` ). – Edison Aug 14 '17 at 23:07
  • Yes. see my answer – Alexander Aug 14 '17 at 23:07

2 Answers2

1

Your Swift3 attempt has nothing to do with your Swift2 code. In the Swift2 code, find(purchaseOrder, chosenKey) returns the index of the String variable, chosenKey in an array of Strings, purchaseOrder. In your Swift3 code you are trying make a tuple from your array of strings and from your search string and look up the elements in a tuple, which will never work, since you are trying to search a tuple with a non-tuple value.

In reality the code should be this simple: you just need to find the index of chosenKey in purchaseOrder using the index(of:) function and use that as the row index.

guard let row = purchaseOrder.index(of: chosenKey) else { return }
let indexPath = IndexPath(row: row, section: 0)
tableView.selectRow(at: indexPath, animated: animated, scrollPosition: .middle)
Alexander
  • 59,041
  • 12
  • 98
  • 151
Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
  • Please advise the etiquette here guys. Alexander gave me the answer first but it needed a simple edit I should've been able to spot, while David's answer was the complete answer including bonus guard. Gulp. – Edison Aug 14 '17 at 23:26
  • Well, it's your choice whose answer you accept, choose the one you felt helped you more. If both answers helped, you can up vote both anyways :) – Dávid Pásztor Aug 14 '17 at 23:27
  • 1
    Sorry David. I had to give it to Alex because he had already laid out all the ground work. I gave you an upvote obviously because I ended up using your guard. I would normally give you the correct answer because you gave all the complete code for it but this case was different. – Edison Aug 14 '17 at 23:36
0

find(_:_:) used to be a free function that took a Collection as its first argument, and the desired element as the second.

Since Swift 2, this functionality is now implemented as an instance method on Collection, called index(of:).

What used to be find(purchaseOrder, chosenKey) is now spelled purchaseOrder.index(of: chosenKey)

Alexander
  • 59,041
  • 12
  • 98
  • 151
  • `collections` isn't a collection. It's a tuple containing a `[String]` and a `[String]`... See my edit – Alexander Aug 14 '17 at 23:10
  • `index = purchseOrder(of: chosenKey)` gives me a `Cannot call value of non-function type '[String]?'` error. Obviously since `purchaseOrder` is not a function type. – Edison Aug 14 '17 at 23:19
  • @tymac That was a typo, it was supposed to be `purchaseOrder.index(of: chosenKey)`. Fixed – Alexander Aug 14 '17 at 23:20