当前位置:网站首页>Swiftui swift tutorial 14 useful array operators

Swiftui swift tutorial 14 useful array operators

2022-06-23 00:39:00 Knowledge fatness

Arrays allow you to aggregate a large number of values into a single collection , These values are then accessed based on their location in the collection .Swift Use type inference to determine the type of data in the array .

Array declaration :

var arr = [2,3,5] 

var arr: [Int] = []
var arr = Array(repeating: 8, count: 3)

1. Add to array

You can add to the end of an array or insert a value at a specific index .append Command or abbreviation += Operators can be used to append , As shown below :

arr.append(5)
arr += [5]

For this task , The two statements are functionally identical . One thing to remember is , In order to use += Operator , You must have an array on the right , Even if it contains only one value . however , If you want to add multiple items to the array , Just add the text to the array , So you can add more values to the array , As shown below :

arr += [9, 8, 7, 6]
[5, 5, 9, 8, 7, 6] 

insert The command can also be used to insert values anywhere , for example :

arr.insert(92, at: 2)
//arr now is [5, 5, 92, 9, 8, 7, 6]

2. Remove from array

You can also delete items from the array . If we want to delete something from a given index , We can do that :

let removedValue = arr.remove(at: 3)
//emptyArray now is [5, 5, 92, 8, 7, 6]
removedValue is 9
原网站

版权声明
本文为[Knowledge fatness]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206222155349272.html