1、枚举类型。swift中的枚举比oc里面的枚举更加的宽泛,不一定只简单的支持int数据类型,还扩展了其他数据类型
//定义枚举
enum swiftEnum {
case OC_Enum
case SWIFT_Enum
case HISTORY_Enum
}
//重定义枚举类型
typealias swiftCategory = swiftEnum
//枚举类型
let abcd = swiftCategory.HISTORY_Enum
print(abcd)
var category :swiftEnum
category = .HISTORY_Enum
category = .SWIFT_Enum
print(category)
输出:
HISTORY_Enum
SWIFT_Enum
SWIFT_Enum
//枚举于switch并用
switch category {
case .HISTORY_Enum:
print(category)
case .OC_Enum:
print(category)
case .SWIFT_Enum:
print(category)
default:
print("...")
}
输出:
SWIFT_Enum
switch category {
case .SWIFT_Enum,.OC_Enum,.HISTORY_Enum:
print("HelloWorld")
default:
print("....")
}
输出:
HelloWorld
2、条件表达式 if/switch
if category == .HISTORY_Enum{
print(category)
}else{
print("other category")
}
输出:
HISTORY_Enum
3、循环
let numberArr = [2,3,5,6,3,2,44]
var sum = 0
for num in numberArr{
sum += num
}
print(sum)
sum = 0
for num in 1...20{
sum += num
}
print(sum)
输出:
65
210
4、函数的语法
4、函数的语法
func returnNumber() -> Int {
var y = 10
func returnY() {
y += 5
}
print(y)
returnY()
print(y)
return y
}
let number = returnNumber()
print(number)
输出:
10
15
15
15
15