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