import Foundation
/**
* 1. 数组的创建
*/
//创建空数组
var someInts = [Int]()
print("someInts is of type [Int] with \(someInts.count) items.")
// 打印 "someInts is of type [Int] with 0 items."
var someInts1 = Array<Int>()
//创建非空数组
var someInts2 = [1,2,3,4]//类型推断
var someInts3 = [Int](arrayLiteral: 1,2,3,4);
var threeDoubles = [Double](count: 3, repeatedValue:0.0)//默认值
// threeDoubles 是一种 [Double] 数组,等价于 [0.0, 0.0, 0.0]
/**
* 2. 数组的访问与修改
*/
var firstItem = someInts3[0]//访问
someInts3[0] = 6 //修改
print(" \(someInts3) ")
// [6, 2, 3, 4]
someInts3.append(5) //末尾添加
print(" \(someInts3) ")
// [6, 2, 3, 4, 5]
someInts3.insert(6, atIndex: 0) //指定位置插入
print(" \(someInts3) ")
// [6, 6, 2, 3, 4, 5]
someInts3[1...3] = [4, 5] //区间赋值,两边个数不一定相等
print(" \(someInts3) ")
// [6, 4, 5, 4, 5]
someInts3.removeAtIndex(2);//删除指定位置
print(" \(someInts3) ")
// [6, 4, 4, 5]
someInts3.removeLast();//删除最后一个
print(" \(someInts3) ")
// [6, 4, 4]
/**
* 3. 数组的遍历
*/
for item in someInts3 {
print(item)
}
//6
//4
//4
for (index, value) in someInts3.enumerate() {
print("Item \(String(index + 1)): \(value)")
}
//Item 1: 6
//Item 2: 4
//Item 3: 4
import Foundation
/**
* 1.创建字典
*/
//标准的创建与初始化
var employee1: Dictionary<String,String> = ["name":"bill", "company":"Microsoft"]
print(employee1)
//省略Dictionary
var employee2: [String: String] = ["name":"bill", "company":"Microsoft"]
print(employee2)
//直接初始化
var employee3 = ["name":"bill", "age":"Microsoft"]
print(employee3)
var dict = ["name":"bill", "age": 50]
//NSMutableDictionary
// 创建一个空字典
var nullDict = [Int: String]()
/**
* 2.访问修改字典
*/
var t = employee1["name"]; //访问
employee3["age"] = "23" //修改
if let oldValue = employee3.updateValue("name", forKey: "name") {
print("The old value for name was \(oldValue).")
}
// 输出 "The old value for name was bill."
employee3["age"] = nil //删除
if let removedValue = employee3.removeValueForKey("name") {
print("The removed employee3 name is \(removedValue).")
} else {
print("The employee3 dictionary does not contain a value for name.")
}
// prints "The removed employee3 name is name."
/**
* 3.遍历
*/
for (v1, v2) in employee2 {
print("\(v1): \(v2)")
}
//company: Microsoft
//name: bill
for v1 in employee2.keys {
print("keys: \(v1)")
}
//keys: company
//keys: name
for v1 in employee2.values {
print("values: \(v1)")
}
//values: Microsoft
//values: bill
import Foundation
/**
* 1.创建集合
*/
var letters = Set<Character>()
//var favoriteGenres: Set<String> = ["Rock", "Classical", "Hip hop"]
//等价于
var favoriteGenres: Set = ["Rock", "Classical", "Hip hop"]
/**
* 2.访问修改集合
*/
//------添加
letters.insert("a")
// letters 现在含有1个 Character 类型的值
letters = []
// letters 现在是一个空的 Set, 但是它依然是 Set<Character> 类型
//------包含
if favoriteGenres.contains("Funk") {
print("I get up on the good foot.")
} else {
print("It's too funky in here.")
}
// 打印 "It's too funky in here."
//------删除
if let removedGenre = favoriteGenres.remove("Rock") {
print("\(removedGenre)? I'm over it.")
} else {
print("I never much cared for that.")
}
// 打印 "Rock? I'm over it."
/**
* 3.遍历
*/
favoriteGenres.insert("Jazz")
for genre in favoriteGenres {
print("\(genre)")
}
// Classical
// Jazz
// Hip hop
for genre in favoriteGenres.sort() {
print("\(genre)")
}
// prints "Classical"
// prints "Hip hop"
// prints "Jazz
/**
* 4.集合运算
*/
/**
* 原先的方法,swift2.2不在使用,之后补上现在的方法
*/
import Foundation
/**
* 1.区间循环
*/
for index in 1...5 {
print("\(index) times 5 is \(index * 5)")
}
// 1 times 5 is 5
// 2 times 5 is 10
// 3 times 5 is 15
// 4 times 5 is 20
// 5 times 5 is 25
//省略index
for _ in 1...10 {
print(1);
}
/**
for循环
* for initialization; condition; increment {
statements
}
*/
/**
* repeat {
statements
} while condition
类似do-while
*/
/**
* 2.switch
不要每个都加break,不存在贯穿,想要贯穿使用fallthrough
*/
//-----字符匹配
let someCharacter:String = "e"
switch someCharacter
{
case "a", "e", "i", "o", "u":
print("\(someCharacter) is a vowel")
case "b", "c", "d", "f", "g", "h", "j", "k", "l", "m",
"n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z":
print("\(someCharacter) is a consonant")
default:
print("\(someCharacter) is not a vowel or a consonant")
}
var switchCondition:String = "bill"
switch switchCondition
{
case "bill":
print("ok bill");
fallthrough
case "Mike":
print("ok Mike");
fallthrough
default:
print("ok default")
}
//-----区间匹配
var price = 100
switch price
{
case 0:
print("免费")
case 10..<50:
print("跳楼价")
case 50..<100:
print("低价")
case 100...500:
print("价格适中")
case 501...999_999_999:
print("抢钱啊,这么贵")
default:
print("当我是沃伦巴菲特啊!")
}
//-----元组匹配 值绑定
let point3D = (0,2,3)
switch point3D
{
case (let x, 2,1):
print("x=\(x)")
case (0, let y, let z):
print("y = \(y), z =\(z)")
case var (x, y, z):
print("x = \(x), y = \(y) z = \(z)")
}
switch point3D
{
case (_, _,3):
print("(0,2,3)")
case (0, let y, let z):
print("y = \(y), z =\(z)")
case var (x, y, z):
print("x = \(x), y = \(y) z = \(z)")
}
//-----元组匹配 where
let point = (1, -1)
switch point
{
case let (x, y) where x < 5 && y > -10:
print("x < 5 && x > -10(x=\(x),y=\(y))")
case let (x, y) where x == y:
print("(\(x), \(y)) is on the line x == -y")
case let (x, y):
print("(\(x), \(y)) is just some arbitrary point")
}
/**
* 3.跳转标签
*/
forLabel:for index1 in 1..<6
{
for index2 in 1...10
{
print(index2)
break forLabel
}
}
//guard