您的位置:首页 > 移动开发 > Swift

Swift-基础-3

2016-06-13 17:23 274 查看
//
//  main.swift
//  Swift-3
//
//  Created by Qsyx on 16/6/13.
//  Copyright © 2016年 Qsyx. All rights reserved.
//

import Foundation

print("Hello, World!")

//数据流

//1.for .. in结构
for i in 0...4
{
print(i)
}
for _ in 0...4
{
print("hello")
}

//2.while 结构
var i : Int = 0
while i < 5
{
print("a")
i += 1
}
//repeat...while 结构 do...while
var j : Int = 0
repeat {
print("b")
j += 1
}
while j<2

if j < 5
{
print("c")
}else if j < 20
{
print("g")
}else
{
print("a")
}

//3. switch 结构: 不存在隐式贯穿

var sw1 : Int = 3
switch sw1{
case 1 :
print("1111")
case 2:
print("2222")
case 3:
print("3333")
case 4:
print("nil")
default:
print("default")
}
//3.1 switch...case 中的区间使用
var sec = 87
switch sec{
case 91...100:
print("a")
case 81...90:
print("b")
default:
print("c")
}
//4 switch...case 中 元组的使用
var yuanzu = (2,3)
switch yuanzu
{
case (1,1):
print("1,1")
case(2,_):
print("2,2")
case(3,_):
print("3,3")
default:
print("default")
}
var yuanzu2 = (2,3)
switch yuanzu2
{
case (let x,3):
print(x)
case(2,_):
print("2,2")
case(3,_):
print("3,3")
default:
print("default")
}

//5. switch...case 中的where条件语句

var yuanzu3 = (2,2)
switch yuanzu3{
case let (x,y) where x == y:
print("相同")
default:
print("不同")
}

//6 switch...case 中的fallthrough语句

var yuanzu4 = (2,2)
switch yuanzu4
{
case (let x,3):
print(x)
case(2,_):
print("2,2")
fallthrough
case(3,_):
print("3,3")
default:
print("default")
}

//==================7.控制转移语句
for i in 0...3
{
for j in 0...3{
if j == 2{
break
}
print("i\(i) j\(j)")
}
}

// 带标签的循环
abc :for ii in 0...3
{
def :for jj in 0...3
{
if jj == 2{
break
}
print("ii\(ii) jj\(jj)")
}
}
/**
*  1⃣️ 函数
*/

//1 函数的定义 与调用
//函数的定义
func add(){
print("add")
}
//函数的调用
add()
//2. 函数参数与返回值
//func 函数名称(函数参数) -> 返回值
//3. 无参函数
func add1(){
print("add1")
}
add1()
//4 . 多参函数
func add2(a : Int, b :Int)
{
print(a,b)
}
add2(4, b: 6)

//5 .无返回值参数
func add3(a : Int)
{
print(a)
}
add3(8)

func add4(a : String) -> Void{
print(a)
}
add4("无返回值")

//6.有返回值函数
func add5(a : Int) -> Int{
return a
}
let ss = add5(0)
print(ss)

//7.元组作为返回值
func add6(a : Int, b : Int) -> (Int,Int)
{
return (a * 10,b * 5)
}
let yuan =  add6(4, b: 3)
print(yuan.1)
//8 可选元组作为返回值
func add7 (a : Int) -> (Int,Int)?
{
if a < 10 {
return nil
}
return (a,10)
}
print(add7(15))
// 8 .指定函数外部参数
/**

- one 是外部用的
- two 是外部用的

- returns: a+b
*/
func add8 (one a : Int,two b :Int) -> Int{
return a + b
}
print(add8(one: 3, two: 1))

//10.忽略函数外部参数
func add9(a: Int, _ b : Int) -> Int{
return a + b
}
print(add9(11, 1))
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息