前端之家收集整理的这篇文章主要介绍了
swift #3 数组,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
swift 数组
数组是一个多值有序list
同一个对象可以在数组中出现多次
**数组必须是同一类型的**
如果需要存放不同类型可以指定 <AnyObject>
Bridging 桥接
var myArray = [1,2,3]
var bridgedArray = myArray as NSArray
var cocoaArray = NSArray(arrayLiteral: 1,3)
var swiftArray = cocoaArray as ? Array<Int> // optional 值
使用
var myArray = [1,3]
myArray.isEmpty
let lastObject = myArray[]
//查找
let index = find(myArray,2)
if let index = index {
//找到了
}else{
//没找到
}
// 添加
myArray.append(4)
myArray.insert(5,atIndex:0)
//删除
myArray.removeIndex(4)
myArray.removeLast()
myArray.removeAll()
//排序
myArray.sort { (a,b) -> Bool in
return a < b
}
定义一个空数组
let rets:[String] = []
强大的switch
var movies = ["starits","alies","11111111111","1"]
for movie in movies {
switch ( movie ) {
case "starts","1" :
println("got starts")
//加入条件判断
case let someone where someone.rangeOfString("i") != nil:
println(" contains s i")
case let nameLength where count(nameLength) > 7:
println(" \(movie) length larger than 7")
default:
println("")
}
}
原文链接:https://www.f2er.com/swift/326578.html