/**
*? 1、簡單值的計算與轉(zhuǎn)換
*/
//無需指定值的類型(賦值的時候系統(tǒng)會自動識別)
let integer1 = 10
let double2 = 10.0
//同時聲明三個相同類型的變量袜匿,每個變量之間用都好隔開更啄,并在最后加上所屬類型
var value1, value2, value3:Double
//值的類型定義
let float3:Float = 30;
print("1、float:",float3," is that right");
//值轉(zhuǎn)字符串
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===")
//創(chuàng)建數(shù)組和字典
var hahArray = ["hah1","hah2","hah3"]
print(hahArray[2])
var hahDictionary = [
"hah1":"h",
"hah2":"a",
"hah3":"h"
]
hahDictionary["hah4"] = "yeah"
print("1穆壕、",hahDictionary["hah2"])
//創(chuàng)建一個空數(shù)組和字典
//指定類型的數(shù)組
let emptyArray = [String]()
let emptyDictionary = [String:Float]()
//如果數(shù)組和字典的類型能夠識別出,不指定類型也行
let emptyArray1 = []
let emptyDictionary1 = [:]
print("==========華麗的分割線=========")
/**
*? 2其屏、控制流:if / switch / for-in / for / while / repeat-while
*/
/********** if *******************/
//實現(xiàn)計算并輸出一個數(shù)組中大于50的元素個數(shù)喇勋,語法如下
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? 判斷結(jié)果為false珍德,如果不是nil练般,會將optionalString2的值賦值給name
var optionalString2:String? = "APPLE"http://變量可選值
//var optionalString2:String? = nil
var greeting = "Hello"
if let name = optionalString2{
greeting = "Hello,\(name)"
}else{
greeting = "HAH"
}
print("2、if",greeting)
/********* switch **************/
//switch支持任意類型的數(shù)據(jù)比較锈候,如String薄料,Integer;switch匹配到相應(yīng)的case后會自動退出switch語句,并不會繼續(xù)向下運行泵琳,因此可以省略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 循環(huán)中使用 ..< 來表示范圍谷市,也可以用傳統(tǒng)的寫法蛔垢;是使用..<創(chuàng)建的范圍不包含上界,包含上界的寫法:...<
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啦桌、使用常規(guī)for循環(huán):",run2)
/******** while **********/
//while的語法和oc相似
var n = 2
while n < 50{
n = n * 2
}
print("2、while",n)
/**
*? 函數(shù)和閉包
*/
//用 func 聲明一個函數(shù) 及皂,語法: func 方法名(參數(shù)名1:參數(shù)類型甫男,參數(shù)名2:參數(shù)類型,) -> 返回值類型{...} 從第二個內(nèi)部方法名開始必須寫上验烧,否則會報錯板驳,如果想要讓第一個方法名也必須寫,只需要 func greet(name name:String,day:String)->Sring{...}
func greet(name:String,day:String) -> String{
return "Hello \(name),today is \(day)day"
}
print("3碍拆、函數(shù)聲明:\( greet("hah",day: "sunny ") ) ===")
//swift中的函數(shù)可以通過使用 “元組” 返回多個值若治,該元組可以用名稱或者數(shù)字表示
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)")
//函數(shù)能嵌套感混,能作為另一個參數(shù)的返回值端幼,也能作為參數(shù)
//1、嵌套,傳入的參數(shù)帶有可變參數(shù)的時候用Int...表示(Int為參數(shù)類型)
func method1(number1:Int...)->Int{
var sum = 0
func add(){
for num in number1{
sum += num
}
}
add()
return sum
}
method1(3,5)
//2弧满、函數(shù)作為返回值,returnFunc()->(Int->Stirng)中的Int為方法里面的int2String的參數(shù)類型婆跑,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
//函數(shù)作為參數(shù)的情況
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)