大佬教程收集整理的这篇文章主要介绍了查找数组的索引并将其附加到数组内的实际元素,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个对象数组。每个对象都有一个称为数字的属性。
struct Age {
var position = 0
}
我也有这些对象的数组:
let age = Age()
let array = [age]
如何将数组中元素的索引附加到位置属性。
类似于:
for i in array {
i.position = i.index
}
基本上,位于位置 0 的数组的第一项应该使 i=0 秒为 1 等。
array
必须是 var
而不是 let
才能可变并使其起作用:
var array = Array(repeaTing: Age(),count: 10) //build a sample array to test with
for index in 0..<array.count {
arraY[index].position = index
}
print(array)
产生以下结果:
[StackOverflowPlayground.Age(position: 0),StackOverflowPlayground.Age(position: 1),StackOverflowPlayground.Age(position: 2),StackOverflowPlayground.Age(position: 3),StackOverflowPlayground.Age(position: 4),StackOverflowPlayground.Age(position: 5),StackOverflowPlayground.Age(position: 6),StackOverflowPlayground.Age(position: 7),StackOverflowPlayground.Age(position: 8),StackOverflowPlayground.Age(position: 9)]
以上是大佬教程为你收集整理的查找数组的索引并将其附加到数组内的实际元素全部内容,希望文章能够帮你解决查找数组的索引并将其附加到数组内的实际元素所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。