import Foundation
// 这行代码就是一个完成的程序
println("Hello, World!")
// 显示声明Float类型
let const:Float = 4
println("\(const)")
// 值永远不会被隐式转换为其他类型,如果你需要把一个值转换成其他类型,请显示转换
let label = "The width is "
let width = 40
let widthLabel = label + String(width)
println("\(widthLabel)")
// 使用 \() 来格式化输出,其中 + 相遇于连接符(就是把他们拼接在一起)
let apples = 3
let oranges = 5
let str = "I have \(apples) apples and " + "\(oranges) oranges!"
let fruitSummary = "I have \(apples + oranges) pieces of fruit."
println ("\(fruitSummary)")
/**
* 数组和字典用法详细探究
*/
// 使用方括号[]来创建数组和字典,并使用下标或者键(key)来访问元素
/* 数组 */
var shoppingList:String[] = ["cat", "water", "dog", "1234"]
// 数组元素数
var count = shoppingList.count
println("数组输出前:\(shoppingList),和数组元素数:\(count)")
shoppingList[1] = "哈哈"
// 判断数组是否为空
if shoppingList.isEmpty {
println("The shoppingList is empty.")
} else {
println("The shoppingList is not empty.")
}
// 添加数组元素
shoppingList.append("添加数组元素")
// 利用运算符添加数组元素
// Alternatively, add a new item to the end of an array with the addition assignment operator (+=):
shoppingList += "Baking Powder"
// shoppingList now contains 4 items
// You can also append an array of compatible items with the addition assignment operator (+=):
shoppingList += ["Chocolate Spread", "Cheese", "Butter"]
// shoppingList now contains 7 items
// 数组范围替换,即4、5、6这三个元素替换为下面的两个元素
shoppingList[4...6] = ["Bananas", "Apples"]
// 像数组中插入元素
shoppingList.insert("插入数组元素",atIndex:0)
println("删除元素前的输出查看:\(shoppingList)")
// 移除数组元素,并且带有返回值,返回被移除的那个元素值,如果不需要,可以不做处理
let mapleSyrup = shoppingList.removeAtIndex(3)
// 移除数组最后一个元素,注释同上
shoppingList.removeLast()
println("数组最后输出:\(shoppingList)")
println("移除数组元素后返回值:\(mapleSyrup)")
println("数组输出第2个字符串后:\(shoppingList[1])\n\n")
// Iterating Over An Array 便利数组
for item in shoppingList {
println("便利数组:\(item)")
if item == "Cheese" {
println("\n")
}
}
// for in 的新用法
for (index,value) in enumerate(shoppingList) {
println("Item\(index + 1):\(value)")
}
// 这行代码就是一个完成的程序
println("Hello, World!")
// 显示声明Float类型
let const:Float = 4
println("\(const)")
// 值永远不会被隐式转换为其他类型,如果你需要把一个值转换成其他类型,请显示转换
let label = "The width is "
let width = 40
let widthLabel = label + String(width)
println("\(widthLabel)")
// 使用 \() 来格式化输出,其中 + 相遇于连接符(就是把他们拼接在一起)
let apples = 3
let oranges = 5
let str = "I have \(apples) apples and " + "\(oranges) oranges!"
let fruitSummary = "I have \(apples + oranges) pieces of fruit."
println ("\(fruitSummary)")
/**
* 数组和字典用法详细探究
*/
// 使用方括号[]来创建数组和字典,并使用下标或者键(key)来访问元素
/* 数组 */
var shoppingList:String[] = ["cat", "water", "dog", "1234"]
// 数组元素数
var count = shoppingList.count
println("数组输出前:\(shoppingList),和数组元素数:\(count)")
shoppingList[1] = "哈哈"
// 判断数组是否为空
if shoppingList.isEmpty {
println("The shoppingList is empty.")
} else {
println("The shoppingList is not empty.")
}
// 添加数组元素
shoppingList.append("添加数组元素")
// 利用运算符添加数组元素
// Alternatively, add a new item to the end of an array with the addition assignment operator (+=):
shoppingList += "Baking Powder"
// shoppingList now contains 4 items
// You can also append an array of compatible items with the addition assignment operator (+=):
shoppingList += ["Chocolate Spread", "Cheese", "Butter"]
// shoppingList now contains 7 items
// 数组范围替换,即4、5、6这三个元素替换为下面的两个元素
shoppingList[4...6] = ["Bananas", "Apples"]
// 像数组中插入元素
shoppingList.insert("插入数组元素",atIndex:0)
println("删除元素前的输出查看:\(shoppingList)")
// 移除数组元素,并且带有返回值,返回被移除的那个元素值,如果不需要,可以不做处理
let mapleSyrup = shoppingList.removeAtIndex(3)
// 移除数组最后一个元素,注释同上
shoppingList.removeLast()
println("数组最后输出:\(shoppingList)")
println("移除数组元素后返回值:\(mapleSyrup)")
println("数组输出第2个字符串后:\(shoppingList[1])\n\n")
// Iterating Over An Array 便利数组
for item in shoppingList {
println("便利数组:\(item)")
if item == "Cheese" {
println("\n")
}
}
// for in 的新用法
for (index,value) in enumerate(shoppingList) {
println("Item\(index + 1):\(value)")
}
// 先写这么多,后续慢慢补上!