jjzjj

RandomAccessCollection

全部标签

arrays - Reverse RandomAccessCollection 的 `_base` 属性是什么?

使用_base属性访问ReverseRandomAccessCollection的元素是一种好习惯吗?letmyArray=[1,2,3]print(myArray.first)//returns1print(myArray.reversed().first)//returns3print(myArray.reversed()._base.first)//return1,whichistheunderlyingbasearray 最佳答案 ReverseRandomAccessCollection(您可以看到itsfullimple

arrays - 将 RandomAccessSlice 转换为 RandomAccessCollection

当我们尝试从Array中检索一系列元素时,我们会返回一个ArraySlice:letarray=[1,3,5,2]letarraySlice=array[..我们可以像这样将它转换回Array类型:letarrayFromSlice=Array(arraySlice)假设您要创建一个返回任何RandomAccessCollection的前3个元素的方法:funcfirst3Elements(_c:T)->T{letslice=c.prefix(3)//COMPILERERROR:non-nominaltype'T'//doesnotsupportexplicitinitializati