程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了查找数组的索引并将其附加到数组内的实际元素大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决查找数组的索引并将其附加到数组内的实际元素?

开发过程中遇到查找数组的索引并将其附加到数组内的实际元素的问题如何解决?下面主要结合日常开发的经验,给出你关于查找数组的索引并将其附加到数组内的实际元素的解决方法建议,希望对你解决查找数组的索引并将其附加到数组内的实际元素有所启发或帮助;

我有一个对象数组。每个对象都有一个称为数字的属性。

 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,请注明来意。
标签: