简体   繁体   English

快速在特定数组元素中添加值

[英]Add values in a particular array element in swift

I have this simple app which asks input from a user and stored in an array. 我有这个简单的应用程序,它要求用户输入并存储在数组中。 Now, for example I have stored an int value of "5" in array[2] in coreData. 现在,例如,我已经在coreData的array [2]中存储了一个int值“ 5”。 Now I want to add another element to the one that I have already in store. 现在,我想向已经存储的元素添加另一个元素。 I have another number "6" to add in array[2] now the result would be array[2] = 11... How do I do it guys? 我要在array [2]中添加另一个数字“ 6”,现在结果将是array [2] = 11 ...我该怎么做? I'm still new to swift... Thanks in advance! 我还是新手,快来...预先感谢!

you can do it this way : 你可以这样:

var arr = [1,3,5,4,6,7]

arr[2] = arr[2] + 6

println(arr[2])

And the result will be 11 . 结果将是11

May be this will help you. 可能会对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM