/**
* 1、简单值的计算与转换
*/
//无需指定值的类型(赋值的时候系统会自动识别)
let integer1 = 10
let double2 = 10.0
//同时声明三个相同类型的变量,每个变量之间用都好隔开,并在最后加上所属类型
var value1, value2, value3:Double
//值的类型定义
let float3:Float = 30;
print("1、float:",float3," is that right");
//值转字符串
let label = "this is a label"
let labelLength = 40
let labelString = label + String(labelLength)
print("1、"+labelString)
//字符串与字符串拼接
let appleCount = 10
let orangeCount = 4
let totalCountStr = "I have \(appleCount + orangeCount) fruits"
print("1、===asdflkj",totalCountStr + ",hah","aaaaaa===")
//还有
print("1、===asdflkj \(totalCountStr),hah,aaaaaa===")
//创建数组和字典
var hahArray = ["hah1","hah2","hah3"]
print(hahArray[2])
var hahDictionary = [
"hah1":"h",
"hah2":"a",
"hah3":"h"
]
hahDictionary["hah4"] = "yeah"
print("1、",hahDictionary["hah2"])
//创建一个空数组和字典
//指定类型的数组
let emptyArray = [String]()
let emptyDictionary = [String:Float]()
//如果数组和字典的类型能够识别出,不指定类型也行
let emptyArray1 = []
let emptyDictionary1 = [:]
print("==========华丽的分割线=========")
/**
* 2、控制流:if / switch / for-in / for / while / repeat-while
*/
/********** if *******************/
//实现计算并输出一个数组中大于50的元素个数,语法如下
let tempArray = [10,59,30,23,58,100]
var count = 0
for score in tempArray{
if score > 50{
count += 1
}
}
print("2、count:", count)
//swift中不会自动将 值 与 0 做比较,也就是说不能使用 if socore{...} 这样的语法,否则会报错;判断值是否为空时,在类型后面加一个问号,标记这个变量的值是可选的,同时与nil做比较
var optionalString:String? = "Hello"
print("2、is that optionalString existed?",optionalString != nil)
//通常是将if与let一起使用,一个可选值可以是一个具体值也可以是nil,如果变量的可选值为nil,let name = optionalString2 判断结果为false,如果不是nil,会将optionalString2的值赋值给name
var optionalString2:String? = "APPLE"//变量可选值
//var optionalString2:String? = nil
var greeting = "Hello"
if let name = optionalString2{
greeting = "Hello,\(name)"
}else{
greeting = "HAH"
}
print("2、if",greeting)
/********* switch **************/
//switch支持任意类型的数据比较,如String,Integer;switch匹配到相应的case后会自动退出switch语句,并不会继续向下运行,因此可以省略break
let string = "apple"
var tempString = ""
switch string{
case "apple":
tempString = "a"
case "p":
tempString = "p"
case "l":
tempString = "l"
default:
tempString = "APPLE"
}
print("2、switch",tempString)
/******* for-in ***********/
//使用for-in遍历字典,语法为:for(key,value)in dictionary{...},括号中的key和value为自己声明的keyName和valueName,可不与dictionary中的key和value同名
let dictionaryArray = [
"array1":[1,2,3,4],
"array2":[5,6,9,8],
"array3":[10,9,8,7]
]
var largest = 0
var largestKey = "key"
for(arrayName,numberArray) in dictionaryArray{
for number in numberArray{
if number > largest{
largestKey = arrayName
largest = number
}
}
}
print("2、for-in:largest number is",largest," largest keyName is",largestKey)
//在 for 循环中使用 ..< 来表示范围,也可以用传统的写法;是使用..<创建的范围不包含上界,包含上界的写法:...<
var runLoopCount = 0
for i in 0..<4{
runLoopCount += 1
}
print("2、for 使用 ..< 表示范围:",runLoopCount)
var run2 = 0
for var j = 0; j < 4; ++j{
run2 += 1
}
print("2、使用常规for循环:",run2)
/******** while **********/
//while的语法和oc相似
var n = 2
while n < 50{
n = n * 2
}
print("2、while",n)
/**
* 函数和闭包
*/
//用 func 声明一个函数 ,语法: func 方法名(参数名1:参数类型,参数名2:参数类型,) -> 返回值类型{...} 从第二个内部方法名开始必须写上,否则会报错,如果想要让第一个方法名也必须写,只需要 func greet(name name:String,day:String)->Sring{...}
func greet(name:String,day:String) -> String{
return "Hello \(name),today is \(day)day"
}
print("3、函数声明:\( greet("hah",day: "sunny ") ) ===")
//swift中的函数可以通过使用 “元组” 返回多个值,该元组可以用名称或者数字表示
func hahFunctionName(scores:[Int]) -> (min:Int, max:Int, sum:Int){
var max = scores[0]
var min = scores[0]
var sum = 0
for score in scores{
sum += score
if score > max{
max = score
}
if score < min{
min = score
}
}
return(min,max,sum)
}
let returnValue = hahFunctionName([2,3,4,5]);
print("3、返回多个值: 最大值\(returnValue.max)或者\(returnValue.1) 最小值\(returnValue.min)或者\(returnValue.0) 总和\(returnValue.sum)或者\(returnValue.2)")
//函数能嵌套,能作为另一个参数的返回值,也能作为参数
//1、嵌套,传入的参数带有可变参数的时候用Int...表示(Int为参数类型)
func method1(number1:Int...)->Int{
var sum = 0
func add(){
for num in number1{
sum += num
}
}
add()
return sum
}
method1(3,5)
//2、函数作为返回值,returnFunc()->(Int->Stirng)中的Int为方法里面的int2String的参数类型,String为最终返回的类型,第一个方法和里面的第二个方法的最终返回类型需保持一致
func returnFunc() -> (Int->String){
func int2String(num:Int) -> String{
return " change to \(num) string"
}
return int2String
}
var intNum = returnFunc()//返回值为int2String(),因此intNum = int2String
intNum(3)
//多个返回值情况
func returnFunc2() -> ((Int,String)->(Int,String)){
func int2String(num:Int,string: String) ->(Int,String){
return (num,"change to \(num) string")
}
return int2String
}
var intNum2 = returnFunc2()//返回值为int2String(),因此intNum2 = int2String
var intAndString = intNum2(3,"hah")
intAndString.1
//函数作为参数的情况
func matchGame(nums:[Int], condition:Int -> Bool) -> Bool{
for items in nums{
if condition(items){
return true
}
}
return false
}
func moreThanTen(num:Int)->Bool{
return num > 10
}
matchGame([3,25,6,23,14], condition: moreThanTen)
func matchCount(nums:[Int], condition:Int -> Bool) -> Int{
var count = 0
for items in nums{
if condition(items){
count += 1
}
}
return count
}
func moreThanFive(num:Int)->Bool{
return num > 5
}
matchCount([3,25,6,23,14], condition: moreThanFive)