Swift笔记-函数

函数的定义与调用

1
2
3
4
5
6
func sayHelloAgain(personName: String) -> String {
return "Hello again, " + personName + "!"
}

print(sayHelloAgain(personName: "Anna"))
// prints "Hello again, Anna!"

函数参数与返回值

无参函数

1
2
3
4
5
func sayHelloWorld() -> String {
return "hello, world"
}
print(sayHelloWorld())
// prints "hello, world"

Swift笔记-控制流

For循环

For-In

你可以使用for-in循环来遍历一个集合里面的所有元素

1
2
3
4
5
6
7
8
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

如果你不需要知道区间序列内每一项的值,你可以使用下划线(_)替代变量名来忽略对值的访问:

1
2
3
4
5
6
7
8
let base = 3
let power = 10
var answer = 1
for _ in 1...power {
answer *= base
}
print("\(base) to the power of \(power) is \(answer)")
// 输出 "3 to the power of 10 is 59049"

Swift笔记-集合类型

Swift 语言提供Arrays、Sets和Dictionaries三种基本的集合类型用来存储集合数据。

数组

创建一个空数组

1
2
3
var someInts = [Int]()
print("someInts is of type [Int] with \(someInts.count) items.")
// 打印 "someInts is of type [Int] with 0 items."

创建一个带有默认值的数组

1
2
var threeDoubles = [Double](count: 3, repeatedValue:0.0)
// threeDoubles 是一种 [Double] 数组,等价于 [0.0, 0.0, 0.0]

Swift笔记-字符串和字符

初始化空字符串

1
2
3
var emptyString = ""               // 空字符串字面量
var anotherEmptyString = String() // 初始化方法
// 两个字符串均为空并等价。

判断该字符串是否为空

1
2
3
4
if emptyString.isEmpty {
print("Nothing to see here")
}
// 打印输出:"Nothing to see here"

Swift笔记-运算符

赋值运算符 (=)

1
2
3
4
let b = 10
var a = 5
a = b
// a 现在等于 10

算术运算符

  • 加法(+)
  • 减法(-)
  • 乘法(*)
  • 除法(/)
1
2
3
4
1 + 2       // 等于 3
5 - 3 // 等于 2
2 * 3 // 等于 6
10.0 / 2.5 // 等于 4.0

加法运算符也可用于String的拼接:

1
"hello, " + "world"  // 等于 "hello, world"

Swift笔记-基础部分

常量和变量

  1. 变量
1
2
3
4
5
6
7
8
9
10
//变量声明
var x = 1

//声明多个变量
var x = 1, y = 2

//带类型声明
var msg: String
//变量赋值
msg = "Hello"
  1. 常量
1
2
//声明常量
let pi = 3.14159

使用hexo搭建github静态博客

网上虽然不乏此类教程,但是实际操作起来还是遇到了一些问题。
总结如下。

以Windows 7系统为例:

登陆Github,创建Reposity

  1. 用户名.github.io, 例如我的是zhouzhuo810.github.io
  2. 点击Setting->Choose Theme->随便选一个Theme->Select->默认Readme
  3. 访问username.github.io可以看到readme的内容即可。

安装Node.js

https://nodejs.org/en/
选择第二个下载安装。