Collection and Index error on Xcode 16

Hi,

After update to Xcode 16 a lot of errors happen, for example:

import Foundation


extension Collection {
    func get(at i: Index) -> Element? {
        return indices.contains(i) ? self[i] : nil
    }
}

Errors:

  • Cannot find type 'Index' in scope
  • Cannot find 'indices' in scope

What is wrong?

Thanks.

Try

extension Collection {
    func get(at i: Int) -> Element? {
        return i < count self[i] : nil
    }
}

I know the above is not what you're looking for but I tried your extension above in playgrounds with no issues.

extension Collection {
    func get(at i: Index) -> Element? {
        return indices.contains(i) ? self[i] : nil
    }
    subscript(safeIndex index: Index) -> Element? {
        return get(at: index)
    }
}

These are the errors after update to Xcode16:

There’s something going with either your project or your tools. You can determine which one it is by trying to reproduce this failure in a small test project.

ps These extensions on Collection are almost always a bad idea. They’re especially bad if you use them on String. There are better ways to deal with collections in Swift. If you’re interested in learning more about that, feel free to start a new thread. Put it in Programming Languages > Swift so that I see it go by.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

I tested in an app in Xcode 16.0. Compiles fine.

I also created a new project, and just added the extension at the end of AppDelegate. Compiles fine.

Collection and Index error on Xcode 16
 
 
Q