How to loop through an array from the second element in elegant way using Swift

后端 未结 4 774
无人及你
无人及你 2020-12-21 06:15

I am a beginner of Swift for iOS development, and want to find an elegant way to loop through an array from the second element.

If it is in Objective-C, I think I ca

4条回答
  •  心在旅途
    2020-12-21 06:31

    you can also use custom subscript to make sure you avoid crashing

    extension Array {
        subscript (gurd index: Index) -> Array.Element? {
            return (index < self.count && index >= 0) ? self[index] : nil
        }
    }
    

    use example:

    let myarray = ["a","b","c","d","e","f","g","h"]
    
     for i in -15...myarray.count+20{
                print(myarray[gurd: i])
            }
    

提交回复
热议问题