swiftui-example icon indicating copy to clipboard operation
swiftui-example copied to clipboard

对数组 Array 的操作

Open jaywcjlove opened this issue 4 years ago • 1 comments

insert 插入

var myArray = ["Steve", "Bill", "Linus", "Bret"]
myArray.insert("jaywcjlove", at: 1)
print(myArray)
// ["Steve", "jaywcjlove", "Bill", "Linus", "Bret"]

append 添加到末尾

var myArray = ["Steve", "Bill", "Linus", "Bret"]
myArray.append("wcj")
print(myArray)
// ["Steve", "Bill", "Linus", "Bret", "wcj"]

合并数组

var array1 = [1,2,3,4,5]
let array2 = [6,7,8,9]

let array3 = array1+array2
print(array3)
array1.append(contentsOf: array2)
print(array1)

jaywcjlove avatar May 08 '21 07:05 jaywcjlove

数组过滤

let words = ["hello", "world", "this", "is", "a", "list", "of", "strings"]
let filtered = words.filter { word in
  return word.count >= 3
}
print(filtered)
// ["hello", "world", "this", "list", "strings"]
let words = ["hello", "world", "this", "is", "a", "list", "of", "strings"]
let filtered = words.filter{ $0.contains("is") }
print(filtered)
// ["this", "is", "list"]

jaywcjlove avatar May 08 '21 16:05 jaywcjlove