How can I change values inside array without a loop Swift

前端 未结 2 1604
感动是毒
感动是毒 2021-01-26 16:59

I have a bool array

For example:

 var myBool: [Bool] = [true, true, false, true ,false]

and I want to change all the elements from inde

2条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-26 17:33

    Any mutable collection responds to replaceSubrange:with:

    var myBool = [true, true, false, true, false]
    myBool.replaceSubrange(1...3, with: Array(repeating: true, count: 3))
    

    or (credits to MartinR)

    var myBool = [true, true, false, true, false]
    let range = 1...3
    myBool.replaceSubrange(range, with: repeatElement(true, count: range.count))
    

提交回复
热议问题