Swift学习散记 1

标签(空格分隔): iOS开发 Swift


Swift已经更新到3.0了,是时候开始学习一下Swift了...

//导入框架
import UIKit
//定义常量
let myFristName: String = "Chris"
let myLastName = "Blue"
//定义变量
var age: Int = 30
var stature = 174
//如果一行中只有一条语句,分号";"可以省略
var a = 10;var b = 20
//打印语句
print("My name is "+myName+" "+myLastName)
print("Age is \(age)")
//数组
var arrayInts = [Int]()
arrayInts = [1,2,3]
print(arrayInts)

var shoppingList: [String]
shoppingList = ["milk","eeg"]
print(shoppingList[0])

//数组的个数
print("The shopping list contains \(shoppingList.count) items.")

//数组是否为空
if shoppingList.isEmpty {
    print("The shopping list is empty.")
} else {
    print("The shopping list is not empty.")
}

//添加数组元素
shoppingList += ["bananas"]
shoppingList += ["apples,chocolate"]
print(shoppingList)

//修改数组元素
shoppingList[1] = "six eegs"
shoppingList[2...3] = ["cheese", "butter"]
print(shoppingList)

//移除数组元素
shoppingList.removeFirst()
shoppingList.removeLast()
shoppingList.removeAll()
print(shoppingList)

你可能感兴趣的:(Swift学习散记 1)