swift-javascriptish-arrays 是扩展 Swift 数组,使得数组类似 JavaScript。
添加多个元素到一个数组:
var fruits = ["Banana", "Orange"] fruits.push("Apple") fruits.push("Kiwi", "Papaya") // fruits == ["Banana", "Orange", "Apple", "Kiwi", "Papaya"]
移除和返回数组的最后一个元素:
var fruits = ["Apple", "Banana", "Orange"] var popped = fruits.pop() // popped == "Orange" // fruits == ["Apple", "Banana"]
移除和返回数组的第一个项:
var fruits = ["Banana", "Orange", "Apple", "Mango"] var shifted = fruits.shift() // shifted == "Banana" // fruits == ["Orange", "Apple", "Mango"]
从数组开始位置添加新元素:
var fruits = ["Banana", "Orange"] fruits.unshift("Lemon","Pineapple") // fruits == ["Lemon","Pineapple", "Banana", "Orange"]
假如多个数组,不改变现有的数组,但是会返回一个新的数组:
var yellowFruits = ["Banana", "Lemon"] var otherFruits = ["Orange", "Apple"] var allFruits = yellowFruits.concat(otherFruits) // allFruits == ["Banana","Lemon", "Orange", "Apple"]
软件简介 swift-javascriptish-arrays 是扩展 Swift 数组,使得数组类似 JavaScript。 push 添加多个元素到一个数组: var fruits = ["Banana", "Orange"] fruits.push("Apple") fruits.push("Kiwi", "Papaya") // fruits == ["Banana", "Orange",