//2.变量常量的定义
//定义变量
var age = 10
//定义常量
let gender = "男"
//3.显示标注类型
//基本类型
//Int Double Float String Character("f") Bool(true false)
//Bool 是true和false
let yanHuaGender = true
var yanHuaAge: Int = 13
var yanHuaWeight: Double = 88
//Character赋值是双引号
let yanHuaGender2: Character = "t"
//5.数组
// 创建数组
var array1 = [12, 345, 67, 245, 23]
var array2 = ["12", "345", "67"]
var array3 = ["abc", 23, 35.3]
var array4: Array<Int> = [23]
var array5: [String] = ["ciqiong", "taishao"]
var array6: Array = [23]
var array7 = [Bool]()
var array8 = Array<Int>()
// 增
array1.append(78)
array1.insert(10000, atIndex: 0)
array1 += [23, 56]
print(array1)
// 删
array1.removeAtIndex(0)
print(array1)
// 改
array1[0] = 1222
print(array1)
// 查
print(array1[2])
var arr1 = [12,234,456]
var arr2 = ["a","ad","dfg"]
var arr3 = ["23","wer",456]
var arr4: Array<Int> = [234,54]
var arr5: [String] = ["qwe","asd","gfg"]
var arr6: Array = [12,"asd",45]
//增
arr1.append(45)
print(arr1)
arr1.insert(10000, atIndex: 0)
print(arr1)
arr1 += [34,8000]
print(arr1)
//删
arr1.removeAtIndex(0)
print(arr1)
//改
arr1[1]=2222
print(arr1)
//查
print(arr1[2])
//6.字典
// 6.字典
var dict1 = [
"key1": "value1",
"key2": 68,
false: "value2"
]
var dict2: Dictionary<Int, Int> = [
23: 567,
56: 78
]
var dict3: Dictionary = [12.34 : 45.67]
var dict4 = [Int: Int]()
var dict5 = Dictionary<String, String>()
// 增加 / 修改
dict5["key1"] = "value1"
dict5["key2"] = "value2"
dict5.updateValue("value3", forKey: "key1")
print(dict5)
// 删除
dict5.removeValueForKey("key1")
print(dict5)
// 查
print(dict5["key2"])
// 如果想要获取字典所有的keys和values,需要通过Array进行强转
let allKeys = Array(dict5.keys)
print(allKeys)
let allValues = Array(dict5.values)
print(allValues)
var dic1=["key1":"value1",
"key2":"value2",
"key3":"value3"
]
print(dic1)
//增加 /修改
dic1["key4"] = "value4"
print(dic1)
dic1.updateValue("value9", forKey: "key1")
print(dic1)
//删除
dic1.removeValueForKey("key1")
print(dic1)
//查询
print(dic1["key2"])
var allkeys2 = Array(dic1.keys)
print(allkeys2)
var allValues2 = Array(dic1.values)
print(allValues2)
//7.元组
var result = (404,"Not Found")
result.1 = "NOT FOUND"
var yanhua = (num: 12,name: "haha",age: 34,des: "sb一个")
yanhua.name = "jj"
print(yanhua)
var (num1,name1,age1,des1)=yanhua
print(name1)
//print中嵌套变量使用 "\(变量名)"
print("num: \(num1) name: \(name1) age: \(age1)")
//8.分支语句
let score = 78
if score > 80 {
print("优秀")
} else if score > 60{
print("jj")
} else {
print("GG")
}
//a.必须有分支满足情况
//b.分支中一定有操作
//c.不需要break
//d.如果想要贯穿,手动添加fallthrough
//e.switch可以进行范围的判断
switch score{
case 90:
print("lal")
case 78:
print("ddd")
default:
print("gg")
}
switch score {
case 0...60:
print("")
case var s where s < 100: //判断
print("")
default:
print("")
}
let point = (12,23)
switch point {
case (12,_): // '_' 代表忽略符
print("")
default:
print("")
}
//9.循环语句
repeat {
print("asdas")
} while false
while false {
print("asdad")
}
for var i=0; i<10; i++ {
print(i)
}
//最常用
//...
//..<
for abc in 0...10 {
print(abc)
}
//数组
let names=["asd","sdf","wer","fgh"]
for name in names {
print(name)
}
//字典
let name2=[
"ju":"de",
"shuang":"shuang1"
]
for (_,value) in name2 {
print("key valuse: \(value)")
}
//10.函数
//无参数无返回值
func test1() {
}
func test2() -> Void {
}
//无参数有返回值
func test3() -> String {
return "asds"
}
//有参数无返回值
func test4(a: Int,b: Float) -> Void {
print(a,b)
}
//有参数有返回值
func test4(a: String,b: Character) -> String {
return "a" + a + "b" + String(b)
}
//有参数 有多个返回值
func test5(a: Int,b: Int) -> (Int,Int) {
return (a+b,a-b)
}
func test6(a: Int,b: Int) ->Int {
return a+b
}
//调用函数
let he = test6(3, b: 5)
//调用有多个参数的函数
let res=test5(8, b: 4)
print(res.0)
print(res.1)
//外部函数名 内部函数名
func test7(a: Int,b: Int) -> Int {
return 0
}
test7(9, b: 3)
//使用下划线修饰第二个参数 可以在外部调用的时候去掉形参
func test8(a: Int,_ b: Int) {
print(a)
}
test8(9, 9)
//inout 默认是let 修改内部值用var去修饰 修改外部值用inout修饰
func test9(inout a: Int) {
a=19
}
var aaa=4
test9(&aaa)
print(aaa)
//函数的嵌套声明
func test10() {
func test11() {
func test12() {
}
}
}
//11.枚举
//enum
enum Season : Int{
case Spring = 4
case Summer
case Autumn
case Winter
}
var s1=Season.Spring
s1 = .Summer
print(s1.rawValue)
//使用原始值创建枚举变量的时候需要注意
var s2=Season(rawValue: 5)
print(s2!)
var sss: Int?
print(sss)
//与switch配合
switch s2! {
case .Spring:
print("春天")
case .Summer:
print("x天")
case .Autumn:
print("q天")
case .Winter:
print("d天")
}
enum Season1 :Int {
case Spring1 = 10
case Summer1
case Autumn1
case Winter1
}
var zzz=Season1.Spring1
print(zzz.rawValue)
var xxx=Season1(rawValue: 12)
print(xxx!)
var vvv : Int?
print(vvv)
/*// 11.枚举
// func enum
enum Season : Int {
case Spring = 4
case Summer
case Autumn
case Winter
}
var s1 = Season.Spring
s1 = .Summer
print(s1.rawValue)
// 使用原始值创建枚举变量时需要注意
var s2 = Season(rawValue: 6)
print(s2!)
// 与switch配合
switch s2! {
case .Spring:
print("夏天")
case .Summer:
print("")
case .Autumn:
print("")
case .Winter:
print("")
}
*/
//12.结构体
//结构体属性要在结构体方法中使用,对象属性要在对象方法中使用
struct Person {
//声明属性
var name: String
var age: Int
//声明结构体属性
static var des: String?
//声明方法
func sayHi() {
print("name: \(name) age: \(age)")
}
//声明结构体方法
static func sayHello() {
print("asdasdasdsad")
}
}
//创建结构体对象
var p1=Person(name: "asd", age: 12)
p1.sayHi()
Person.sayHello()
Person.des = "asd"
struct Student {
var name: String?
var gender: String?
var age: Int?
var hobby: String?
init(){
}
init(name: String, age: Int){
self.name=name;
self.age=age;
}
init(name: String, gender: String, age: Int,hobby: String){
self.name=name
self.gender=gender
self.age=age
self.hobby=hobby
}
}
var stu1=Student(name: "", gender: "", age: 12, hobby: "")
print(stu1)
var stu2=Student(name: "", age: 13)
print(stu2)
// 12.结构体
/*struct Person {
// 声明属性
var name: String
var age: Int
// 声明结构体属性
static var des: String?
// 声明方法
func sayHi() {
print("name: \(name) age: \(age)")
}
// 声明结构体方法
static func sayHello() {
print("你好,我叫大华,我是人, \(self.des)")
}
}
// 创建结构体对象
var p1 = Person(name: "dahuazi", age: 38)
p1.age = 83
print(p1.age)
p1.sayHi()
Person.des = "aaaaaaaaaaaaaa"
Person.sayHello()
struct Student {
var name: String?
var gender: String?
var age: Int?
var hobby: String?
// 构造函数
init(name: String, age: Int) {
self.name = name
self.age = age
}
init(name: String, gender: String, age: Int, hobby: String) {
self.name = name
self.gender = gender
self.age = age
self.hobby = hobby
}
init() {
}
}
let sss1 = Student()*/
struct Teacher {
var name: String?
var age: Int?
var gender: String?
static var des: String?
func test1(name: String,age: Int) {
print("name: \(name),age: \(age)")
}
static func sayHello(){
print("des: \(des)")
}
init(name: String,age: Int) {
self.name=name
self.age=age;
}
init(){
}
}
var tea1=Teacher(name: "asd", age: 12)
print(tea1)
var tea2=Teacher()
tea2.test1("qwe", age: 23)