'characters is unavailable' please use string directly

后端 未结 1 1695
被撕碎了的回忆
被撕碎了的回忆 2021-01-14 07:20

I can\'t figure out how to fix it. I just want to understand how it works and what should be replaced.

I\'ve already tried to delete characters., but it

相关标签:
1条回答
  • 2021-01-14 07:26

    Your code should work as it is if you delete the characters I suggest you create a new playground file. Btw you can simply use RangeReplaceableCollection mutating method popLast and iterate while your string is not empty to avoid calling your collection count property multiple times:

    var shrinking = "hello"
    repeat {
        print(shrinking)
        shrinking.popLast()
    } while !shrinking.isEmpty
    

    This will print

    hello

    hell

    hel

    he

    h

    or using removeLast method but it requires the string not to be empty so you would need to check if the string is empty at before the closure:


    var shrinking = "hello"
    
    while !shrinking.isEmpty {
        print(shrinking)
        shrinking.removeLast()
    }
    
    0 讨论(0)
提交回复
热议问题