swift 的if语句,switch语句,元组,函数

前端之家收集整理的这篇文章主要介绍了swift 的if语句,switch语句,元组,函数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

swift if语句,switch语句,元组,函数


//
//  main.swift
//  L00flow
//
//  Created by hins on 16/10/26.
//  Copyright © 2016年 hins. All rights reserved.
//

import Foundation

print("Hello,World!")

var str = "Hello";

for(var i=0;i<5;i=i+1){
    print(i)
}

//流程控制
//=======if语句
for i in 1...10{
    if(i%2==0){
        print(i);
    }
    if(i != 3){
        print("ccccc")
    }
    if(i>5 && i<=7){
        print("jjjj")
    }else{
        print("ttttt")
    }
}

//========switch语句
var index = 3;
switch (index){
case 1 :
        print(index);
        break;
case 2 :
    print(index);
    break;
case 3 :
    print(index);
    break;
default:
    break;
    
}

//=============switch语句
var valChar = "bc";
switch (valChar){
case "a" :
    print(valChar);
    break;
case "b" :
    print(valChar);
    break;
case "bc" :
    print(valChar);
    break;
default:
    break;
    
}


//==========元组(区间)
let somePoint = (1,1)
switch somePoint {
case (0,0):
    print("(0,0) is at the origin")
case (_,0):
    print("(\(somePoint.0),0) is on the x-axis")
case (0,_):
    print("(0,\(somePoint.1)) is on the y-axis")
case (-2...2,-2...2):
    print("(\(somePoint.0),\(somePoint.1)) is inside the Box")
default:
    print("(\(somePoint.0),\(somePoint.1)) is outside of the Box")
}
// prints "(1,1) is inside the Box"


//============函数
//无返回值
func getString(name:String){
    print("wwwwww,\(name)")
}

//调用
getString("xuan");

//有返回值
func getValueBB(name:String,age:Int) -> String {
    print(name);
    return name;
}

//调用 (有些参数提示要带标签 如:age:23,否则报错)
var a2 = getValueBB("nununu",age:23);
print(a2)


func getNums() -> (Int,Int){
    print("hello");
    return (2,3);
}

//调用
let (a,b) = getNums();
print(b);

var fun22 = getValueBB;//函数赋值给变量

_ = fun22("test",age:45);//_ 下划线表示不接收返回值

//函数闭包 (函数中包含函数)

func namefunc(){
    print("函数里面有一个函数");
    func namefunc333(){
        print("hahaha")
    }
}

猜你在找的Swift相关文章