Peter71
Peter71

Reputation: 2310

How to use Int32 as Index without an error?

I have an array which is large, but doesn't need an Int with 8 Byes. So I decided to use Int32 as index. I also defined an extension to avoid error messages, that you can't use Int32 but to convert it to Int:

extension Array {
    subscript(index: Int32) -> Element? {
        get {
            if index < 0 || Int(index) >= count {
                return nil
            }
            return self[Int(index)]
        }
        set(newValue) {
            if let newValue = newValue, index >= 0 && Int(index) < count {
                self[Int(index)] = newValue
            }
        }
    }
}

Unfortunately this doesn't have any effect. Any idea?

Upvotes: 0

Views: 176

Answers (0)

Related Questions