1

I have problem with array of GMSMarker. when i run my code its shows "fatal error: Array index out of range". I am going to remove markers from google map. i don't understand why this error comes. This is simple but pls help me to catch problem.

var MarkerList = [GMSMarker]()

    if(MarkerList.count > 0){
        for var j = 0 ; j < MarkerList.count ; j++ {
            dispatch_async(dispatch_get_main_queue()) {
                self.MarkerList[j].map = nil    
            }
        }
    }
3
  • Did you try to print a log that says exactly what is the size of your array and the current index (j) for each iteration? Commented Dec 15, 2015 at 7:40
  • remove the main queue, why are you setting it to nil on main queue.? UI related updates should be done on mainqueue Commented Dec 15, 2015 at 7:42
  • from which queue do you run your code? Commented Dec 15, 2015 at 7:49

1 Answer 1

2

You should run the whole for loop on the main thread. Or you can go even better and use the new forEach function in Swift2.

Before:

if(MarkerList.count > 0){
    for var j = 0 ; j < MarkerList.count ; j++ {
        dispatch_async(dispatch_get_main_queue()) {
            self.MarkerList[j].map = nil    
        }
    }
}

After:

dispatch_async(dispatch_get_main_queue()) {
    MarkerList.forEach { $0.map = nil }
}
Sign up to request clarification or add additional context in comments.

Comments

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.