RemoveAtIndex crash from swift array

后端 未结 5 731
半阙折子戏
半阙折子戏 2021-01-25 20:43

I have an array of letters, and want to match the characters against the letters and then do something to that letter (in this case turn it yellow) and then remove that matched

5条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-25 21:40

    May i suggest a different approach where instead of removing from the initial array you make a new one, from which you can exclude the characters you don't want to appear in your array in the first place.

    PS: keep in mind the following code works with Swift 2.0 Xcode 7 beta 6

    var letters : [Character] = ["a","b", "c"]
    
    var lettersToBeRemoved : [Character] = ["a"]
    
    var newLetters : [Character] = []
    
    for i in letters
    {
        for j in lettersToBeRemoved
        {
            if i == j
            {
                continue
            }
                newLetters.append(i)
        }
    
    }
    print(newLetters)
    

提交回复
热议问题