3

I have gone through this SO question and came to know this happens due to read and write at the same time. But in my case I am not able to figure out where I am reading and writing to my array at the same time.

What I am doing is I am removing a subrange from an array before inserting to it. e.g:

var createGameOptions = [GOCreateGameDetailsModel]()
for attribute in model.gameAttributes! {
     let model = GOCreateGameDetailsModel.init(title: attribute.attribute_name!, image: nil, value: "", imageUrl: attribute.attribute_icon)
     createGameOptions.append(model)
}
if (createGameModel?.createGameOptions?.count)! > 3 {
    createGameModel?.createGameOptions?.removeSubrange(2...((createGameModel?.createGameOptions?.count)! - 2))
}
createGameModel?.createGameOptions?.insert(contentsOf: createGameOptions, at: 2) 

Any help will be highly appreciated.

iPeter
  • 1,330
  • 10
  • 26

2 Answers2

7

You should try updating this line

createGameModel?.createGameOptions?.removeSubrange(2...((createGameModel?.createGameOptions?.count)! - 2))

To

let count = (createGameModel?.createGameOptions?.count)!
createGameModel?.createGameOptions?.removeSubrange(2...(count - 2))

Try and share the results

Bhavin Kansagara
  • 2,866
  • 1
  • 16
  • 20
5

in Swift 4.2 warnings related to exclusive access become errors, This means project with these warnings will never compile on Swift 4.2

Warnings comes when a mutating method that modifies a variable is passed a non-escaping closure that reads from the same variable. For example:

var result = Data(count:prf.cc.digestLength)
let count = result.count
let status = result.withUnsafeMutableBytes {
            // can not use directly result.count inside this non escaping closure. must computed before using count
}

For more detail about this please check this article. exclusive access erros

Muhammad Shauket
  • 2,643
  • 19
  • 40