0

Is there a way to set the maximum index size of an array. For example I have an array of UIImage but I only want the array to store 6 images. How would I set a restriction on that array so it can only hold 6 images

3

2 Answers 2

1

There is no such functionality. You would have to implement it yourself:

if array.count < 6 {
    array.append(element)
}

or perhaps:

while array.count >= 6 {
    array.removeFirst()
}
array.append(element)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I realized this just after I posted the question
0

Initialize your array with size of 6 and then do any one of the following checks:

  • Check element count of the array before inserting new element
  • You can surround your insertion code with try / catch block with 'ArrayIndexOutOfBounds' Exception being handled

1 Comment

You can't catch such an error in Swift.. Only functions which throw can throw an error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.