Search code examples
arraysswiftarray-indexing

IOS/Swift: How to indicate the first position of the $0 in the .firstIndex ? 'Cannot convert value of type 'Any' to expected argument type 'String''


I would like to search a value in an array of arrays, like in this example but I don't know how to indicate it "the position of the first element in $0" ?

var programArray: [Any] = []

let slots: String =  "2021-14-09 08:00:00|2021-14-09 09:00:00|ACCUEIL CAFE|Amphi A#2021-14-09 09:00:00|2021-14-09 10:00:00|PLENIERE|Amphi A#2021-14-09 10:00:00|2021-14-09 12:00:00|WORKSHOP|Salle Besse#2021-14-09 12:00:00|2021-14-09 14:00:00|DEJEUNER|Cantine#2021-14-09 14:00:00|2021-14-09 16:00:00|TESTDRIVES|XXX#2021-14-09 16:00:00|2021-14-09 17:00:00|CLOTURE|Amphi A#"

//Convert Slots in array
let stringArray = slots.components(separatedBy: "#")
if stringArray.count > 1 {
    for i in 0..<stringArray.count {
        let intermediateTwo = stringArray[i]
        let strinArrayTwo = intermediateTwo.components(separatedBy: "|")
        
        if strinArrayTwo.count > 1 {
            programArray.append(strinArrayTwo)
            
        } else {
            print("not found")
        }
    }
} else {
    print("not found")
}

//Remove other dates than today
let index = programArray.firstIndex(where: {$0 == "2021-14-09"}) //PROBLEM: Cannot convert value of type 'Any' to expected argument type 'String'
    //access index here
programArray.remove(at: index)

Thanks in advance


Solution

  • ⚠️ Don't use contain() on the joined string!

    The other answer and the comments are suggesting to use contain on the joined section. Although it may return the correct result in the specific case, Using contain will result in a fuzzy search and may result in undesired behavior like finding an unrelated object


    ✅ Go the right way

    First, try to clear things up:

    var matrix = slots.split(separator: "#").map { $0.split(separator: "|") }
    dump(matrix)
    

    This will give you a matrix of the data:

    ▿ 6 elements
      ▿ 4 elements
        - "2021-14-09 08:00:00"
        - "2021-14-09 09:00:00"
        - "ACCUEIL CAFE"
        - "Amphi A"
      ▿ 4 elements
        - "2021-14-09 09:00:00"
        - "2021-14-09 10:00:00"
        - "PLENIERE"
        - "Amphi A"
      ▿ 4 elements
        - "2021-14-09 10:00:00"
        - "2021-14-09 12:00:00"
        - "WORKSHOP"
        - "Salle Besse"
      ▿ 4 elements
        - "2021-14-09 12:00:00"
        - "2021-14-09 14:00:00"
        - "DEJEUNER"
        - "Cantine"
      ▿ 4 elements
        - "2021-14-09 14:00:00"
        - "2021-14-09 16:00:00"
        - "TESTDRIVES"
        - "XXX"
      ▿ 4 elements
        - "2021-14-09 16:00:00"
        - "2021-14-09 17:00:00"
        - "CLOTURE"
        - "Amphi A"
    

    Then search for the index you need and remove it like:

    if let searchedIndex = matrix.firstIndex(where: { $0.first?.hasPrefix("2021-14-09") == true }) {
        matrix.remove(at: searchedIndex)
    }
    

    Also, You can join them back to the original format like:

    let updatedSlots = matrix.map { $0.joined(separator: "|") }.joined(separator: "#")