xiwenAndlejian / my-blog

Java基础学习练习题
1 stars 0 forks source link

iOS学习笔记(一)基础篇(上) #7

Open xiwenAndlejian opened 6 years ago

xiwenAndlejian commented 6 years ago

学习笔记来自https://www.hackingwithswift.com/read/0

环境版本:

var & let 变量与常量

var:变量,可以随时修改变量代表的值。

let:常量,赋值之后不能修改这个常量代表的值。

var game = "Dark Soul"
let action = "Hello"

game = "Dark Soul 2" // ✅编译可以通过
action = "Hate" // ❎编译无法通过,常量无法修改

// 如下代码会在 playground 报错,无法通过编译,但实际是能使用的
// 参考https://stackoverflow.com/questions/51362893/change-in-xcode-10-playgrounds-variable-initialization-change-are-xcode-10-play
let constant: String
constant = "常量先声明,再赋值"

// ❎如下代码在 iOS12 无法通过编译
var test
test = "Hello World"
// ✅如下代码即可通过编译,置于为何,之后再做分析
var test: String?
test = "Hello World"

数据类型

Swift中不需要声明变量类型,但不代表变量就没有类型

var test = "Hello World"
type(of: test) // String.Type

Swift会推断我们需要的变量类型

也可以显示的给变量定义它的类型,例如:var test: String = "Hello World"

数值

var num1 = 0
var num2 = 0.0
type(of: num1)// Int.Type
type(of: num2)// Double.Type,注意:没有显示定义类型时,Swift会自动为浮点型推断 Double 类型,而不是 Float

Double的精度要高于Float,但是也只是有限的高

布尔

Swift中的关键字为Bool

运算符

与其他语言类似的运算符+添加,-减法,*乘法,/除法,=赋值等等

var a = 1.1
var b = 2.2
var c = 5
var d = a + b// ✅3.3
var e = a + 3// ✅4.1
var f = a + c// ❎编译不通过

为什么同是变量a+一个整数,a+3能通过编译,而a+c(一个整数变量)却不行?

推测:由于Swift不同类型不能进行运算,aDouble,且cInt,因此不能进行运算。而在变量e计算中,Swift推断此处的1Double类型,因此才能通过编译。

比较运算符:大于(>),大于或等于(>=),小于(<),等于(==),否(!),不等(!=

字符串

字符串插值:在字符串中组合变量和常量

var name = "Jack"
print("My name is \(name)") // 输出:My name is Jack
print("My name is " + name) // 同样也可以使用 +

// 甚至还能在字符串中进行计算
var age = 23
"You are \(age) years old. In another \(age) years you will be \(age * 2)." //输出:You are 23 years old. In another 23 years you will be 46.

数组

var songs1 = ["Shake it Off", "You Belong with Me", "Back to December"]
// 约定数组内元素类型 String
var songs2: [String] = ["Shake it Off", "You Belong with Me", "Back to December"] 
// 数组可包含任意元素
var songs3: [Any] = ["Shake it Off", "You Belong with Me", "Back to December", 3]

创建空数组

var songs1 = [String]()// 常用
var songs2: [String] = []//与基本类型相似的声明赋值方式

拼接数组

var songs = ["Shake it Off", "You Belong with Me", "Love Story"]
var songs2 = ["Today was a Fairytale", "Welcome to New York", "Fifteen"]
var both = songs + songs2

both += ["Everything has Changed"]

字典

键值对形式,类似Java中的Map数据类型

var person = [
                "first": "Taylor",
                "middle": "Alison",
                "last": "Swift",
                "month": "December",
                "website": "taylorswift.com"
            ]

person["middle"]
person["month"]
// emoji
var dict: [String: String] = ["👻": "Ghost",
                              "💩": "Poop",
                              "😠": "Angry",
                              "😱": "Scream",
                              "🤓": "Nerdy"]
dict["👻"]

if & else

var action: String
var stayOutTooLate = true
var nothingInBrain = true

if stayOutTooLate && nothingInBrain {
    action = "cruise"
} else if !stayOutTooLate && nothingInBrain {
    // do someting
} else {
    // do other
}

for 循环

for i in 1 ... 10 {
    print(i) // 从1到10
}

for i in 1 ..< 10 {
    print(i) // 从1到9
}
// 打印整个数组
var songs = ["Shake it Off", "You Belong with Me", "Love Story"]
for i in 0 ..< songs.count {
    print(songs[i])
}
// 不需要获取当前索引,只需要执行固定次数时
for _ in 1 ... 10 {
    print("人类的本质是复读机")// 人类的本质是复读机X10
}

..<被称为半开区间

while 循环

var counter = 0

while true {
    print("Counter is now \(counter)")
    counter += 1

    if counter == 556 {
        break
    }
}

switch

let liveAlbums = 2

switch liveAlbums {
case 0:
    print("You're just starting out")

case 1:
    print("You just released iTunes Live From SoHo")

case 2:
    print("You just released Speak Now World Tour")

default:
    print("Have you done something new?")
}

// 使用区间
let studioAlbums = 5

switch studioAlbums {
case 0...1:
    print("You're just starting out")

case 2...3:
    print("You're a rising star")

case 4...5:
    print("You're world famous!")

default:
    print("Have you done something new?")
}