[Solved] how to check if there is any string from index 0 to 5 in an array of type Any [closed]


You can first slice your array with a range. Then filter the sliced array by checking if the elements can be type cast to string. If the count of filtered array is equal to your range then you can say first n elements are of type String.

This is test code I have tried:

let testArray: [Any] = ["a", 2, 3,"S", 4, 2, 5, 1.0, "a"]

let slicedArrayCount = Array(testArray[0..<5]).filter { (object) -> Bool in
    if let _ = object as? String {
        return true
    }
    return false
}.count

1

solved how to check if there is any string from index 0 to 5 in an array of type Any [closed]