[绍棠_swift] Swift3.0的学习枚举、断言、函数嵌套
2017-07-21 13:43
281 查看
//
// ViewController.swift
// Learn_Swift_2
//
// Created by Coco on 2017/7/20.
// Copyright © 2017年 Coco. All rights reserved.
//
import UIKit
typealias Config = (RAM:
Int, CPU: String, GPU:
String)
// 枚举
enum enumType {
case North
case South
case East
case West
case Center
case North_South, North_East
// 多个成员也可以出现在同一行上
}
// 映射到整型
enum Movement: Int {
case Left = 0
case Right = 1
case Top = 2
case Bottom = 3
}
// 同样你可以与字符串一一对应
enum House: String {
case Baratheon =
"Ours is the Fury"
case Greyjoy =
"We Do Not Sow"
case Martell =
"Unbowed, Unbent, Unbroken"
case Stark =
"Winter is Coming"
case Tully =
"Family, Duty, Honor"
case Tyrell =
"Growing Strong"
}
// 关联枚举,
可关联变化的值
enum barcode {
case UPCA(Int,Int,Int)
//关联了3个参
4000
数
case QRCode(String)
//关联了一个字符串
}
enum Trade { //
买和卖交易物品和数量
case Buy(stock:
String, amount: Int)
case Sell(stock:
String, amount: Int)
}
// 嵌套枚举
enum Character {
enum Weapon {
case Bow
case Sword
case Lance
case Dagger
}
enum Helmet {
case Wooden
case Iron
case Diamond
}
case Thief
case Warrior
case Knight
}
enum Desktop {
case Cube(Config)
case Tower(Config)
case Rack(Config)
}
class ViewController: UIViewController {
override
func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor =
UIColor.white;
learnTest1(num1: 1, num2: 2)
learnTest2(HaHa: 6, HeHe: 8)
learnTest3(num1: 1)
let num =
learnTest3(num1: 1, num2: 20)
/// 后面一个参数默认是以形参为外部参数的
print("num的值
\(num)")
learnTest5(num1:1, num2:2)
var n1 = 10;
var n2 = 11;
swap(num1: &n1, num2: &n2)
//加上类似C语言的取地址符号&
print("n1 =
\(n1), n2 =
\(n2)")
// 函数类型(函数可一个当作参数,定义变量,
作为返回值)
var learnFun1 =
learnTest5
learnFun1(10, 10)
let num0 =
learnFun2(fun: (11, 11))
print("learnFun2
\(num0)")
/// 断言
// var a = 10
// assert(a<5, "a的值不符合要求")
/// 访问枚举
var direct =
enumType.North
direct = .North_East
// 因为前面访问过,
后面自动推断,
可不写枚举名
switch direct {
case .North:
print("Lots of planets have a north")
case .East:
print("Lots of planets have a east")
case .West:
print("Lots of planets have a west")
case .North_East:
print("Lots of planets have a north_east")
default:
print("default")
}
var productBarcode =
barcode.UPCA(1, 2, 3)
switch productBarcode {
case .UPCA(1, 1, 1):
print("关联枚举1")
case .UPCA(2, 2, 2):
print("关联枚举2")
case .UPCA(1, 2, 3):
print("关联枚举正确")
default:
print("关联枚举错误")
}
var characterMenu =
Character.Weapon.Dagger;
/// ------------------------------------------------------
let trade =
Trade.Buy(stock:
"APPL", amount: 100)
if
case let Trade.Buy(stock, amount) = trade {
print("buy
\(amount) of
\(stock)")
}
let aTower =
Desktop.Tower(selectGPU(selectCPU(selectRAM((0,
"",
"") as
Config))))
let bTower =
Desktop.Tower((0,
"","")
as Config)
}
override
func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func learnTest1(num1:Int, num2:Int) ->
Void {
print(num1+num2)
}
// 外部参数,
方便记忆
func learnTest2(HaHa num1 :
Int, HeHe num2 :
Int) -> Int {
return
Int(num1 + num2)
}
// 默认参数
func learnTest3(num1 :
Int, num2 : Int = 10) ->
Int {
return
Int(num1+num2)
}
// 常量与变量参数. (默认情况下,Swift的参数都是常量参数,是不能修改的)
func learnTest4(num1 :
Int, num2 : Int) ->
Void {
// num2 = 6 //
这句话是错误的,
不能修改
print(num1+num2)
}
func learnTest5( num1 :
Int, num2 : Int) {
// 这样修改
var num = num2
num = 6
print("test5的输出值\(num1+num)")
}
// 经典的交换函数
func swap( num1:inout
Int, num2:inout
Int) {
let tmp = num1
num1 = num2
num2 = tmp
}
func learnFun2(fun:(num1:Int, num2:Int)) ->
Int { //
函数作为参数
let num0 = fun
print("learnFun2
\(num0)")
return 5;
}
/// 函数嵌套
func doSomething(num :
Int) {
func walk(){
print("working")
}
func eat() {
print("sit back")
}
if num == 1 {
walk()
} else {
eat()
}
}
func selectRAM(_ config:
Config) -> Config {
return (RAM: 32, CPU: config.CPU, GPU: config.GPU)
}
func selectCPU(_ config:
Config) -> Config {
return (RAM: config.RAM, CPU:
"3.2GHZ", GPU: config.GPU)
}
func selectGPU(_ config:
Config) -> Config {
return (RAM: config.RAM, CPU:
"3.2GHZ", GPU: "NVidia")
}
}
// ViewController.swift
// Learn_Swift_2
//
// Created by Coco on 2017/7/20.
// Copyright © 2017年 Coco. All rights reserved.
//
import UIKit
typealias Config = (RAM:
Int, CPU: String, GPU:
String)
// 枚举
enum enumType {
case North
case South
case East
case West
case Center
case North_South, North_East
// 多个成员也可以出现在同一行上
}
// 映射到整型
enum Movement: Int {
case Left = 0
case Right = 1
case Top = 2
case Bottom = 3
}
// 同样你可以与字符串一一对应
enum House: String {
case Baratheon =
"Ours is the Fury"
case Greyjoy =
"We Do Not Sow"
case Martell =
"Unbowed, Unbent, Unbroken"
case Stark =
"Winter is Coming"
case Tully =
"Family, Duty, Honor"
case Tyrell =
"Growing Strong"
}
// 关联枚举,
可关联变化的值
enum barcode {
case UPCA(Int,Int,Int)
//关联了3个参
4000
数
case QRCode(String)
//关联了一个字符串
}
enum Trade { //
买和卖交易物品和数量
case Buy(stock:
String, amount: Int)
case Sell(stock:
String, amount: Int)
}
// 嵌套枚举
enum Character {
enum Weapon {
case Bow
case Sword
case Lance
case Dagger
}
enum Helmet {
case Wooden
case Iron
case Diamond
}
case Thief
case Warrior
case Knight
}
enum Desktop {
case Cube(Config)
case Tower(Config)
case Rack(Config)
}
class ViewController: UIViewController {
override
func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor =
UIColor.white;
learnTest1(num1: 1, num2: 2)
learnTest2(HaHa: 6, HeHe: 8)
learnTest3(num1: 1)
let num =
learnTest3(num1: 1, num2: 20)
/// 后面一个参数默认是以形参为外部参数的
print("num的值
\(num)")
learnTest5(num1:1, num2:2)
var n1 = 10;
var n2 = 11;
swap(num1: &n1, num2: &n2)
//加上类似C语言的取地址符号&
print("n1 =
\(n1), n2 =
\(n2)")
// 函数类型(函数可一个当作参数,定义变量,
作为返回值)
var learnFun1 =
learnTest5
learnFun1(10, 10)
let num0 =
learnFun2(fun: (11, 11))
print("learnFun2
\(num0)")
/// 断言
// var a = 10
// assert(a<5, "a的值不符合要求")
/// 访问枚举
var direct =
enumType.North
direct = .North_East
// 因为前面访问过,
后面自动推断,
可不写枚举名
switch direct {
case .North:
print("Lots of planets have a north")
case .East:
print("Lots of planets have a east")
case .West:
print("Lots of planets have a west")
case .North_East:
print("Lots of planets have a north_east")
default:
print("default")
}
var productBarcode =
barcode.UPCA(1, 2, 3)
switch productBarcode {
case .UPCA(1, 1, 1):
print("关联枚举1")
case .UPCA(2, 2, 2):
print("关联枚举2")
case .UPCA(1, 2, 3):
print("关联枚举正确")
default:
print("关联枚举错误")
}
var characterMenu =
Character.Weapon.Dagger;
/// ------------------------------------------------------
let trade =
Trade.Buy(stock:
"APPL", amount: 100)
if
case let Trade.Buy(stock, amount) = trade {
print("buy
\(amount) of
\(stock)")
}
let aTower =
Desktop.Tower(selectGPU(selectCPU(selectRAM((0,
"",
"") as
Config))))
let bTower =
Desktop.Tower((0,
"","")
as Config)
}
override
func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func learnTest1(num1:Int, num2:Int) ->
Void {
print(num1+num2)
}
// 外部参数,
方便记忆
func learnTest2(HaHa num1 :
Int, HeHe num2 :
Int) -> Int {
return
Int(num1 + num2)
}
// 默认参数
func learnTest3(num1 :
Int, num2 : Int = 10) ->
Int {
return
Int(num1+num2)
}
// 常量与变量参数. (默认情况下,Swift的参数都是常量参数,是不能修改的)
func learnTest4(num1 :
Int, num2 : Int) ->
Void {
// num2 = 6 //
这句话是错误的,
不能修改
print(num1+num2)
}
func learnTest5( num1 :
Int, num2 : Int) {
// 这样修改
var num = num2
num = 6
print("test5的输出值\(num1+num)")
}
// 经典的交换函数
func swap( num1:inout
Int, num2:inout
Int) {
let tmp = num1
num1 = num2
num2 = tmp
}
func learnFun2(fun:(num1:Int, num2:Int)) ->
Int { //
函数作为参数
let num0 = fun
print("learnFun2
\(num0)")
return 5;
}
/// 函数嵌套
func doSomething(num :
Int) {
func walk(){
print("working")
}
func eat() {
print("sit back")
}
if num == 1 {
walk()
} else {
eat()
}
}
func selectRAM(_ config:
Config) -> Config {
return (RAM: 32, CPU: config.CPU, GPU: config.GPU)
}
func selectCPU(_ config:
Config) -> Config {
return (RAM: config.RAM, CPU:
"3.2GHZ", GPU: config.GPU)
}
func selectGPU(_ config:
Config) -> Config {
return (RAM: config.RAM, CPU:
"3.2GHZ", GPU: "NVidia")
}
}
相关文章推荐
- Swift 3.0基础学习之枚举类型
- Swift 3.0 学习笔记-3-集合类型
- Swift 3.0 学习 -- 大写和小写字符串(Uppercase and Lowercase Strings)
- Swift3.0学习笔记-Automatic Reference Counting
- Swift3.0学习笔记-String and Characters
- Swift 3.0 学习笔记-4-控制流
- 对照Java学习Swift--枚举(Enumerations)
- Swift编程语言学习1.7——断言
- Swift3.0-枚举(enum)和结构体(struct)
- Swift3.0学习之GCD
- Swift学习-枚举(Enumerations)的使用方法
- [绍棠_swift] Swift中assert(断言)的使用
- Swift 3.0基础学习之闭包
- Swift 基础学习(枚举基本语法)
- Swift学习之八:断言(Assertions)
- Swift3.0学习实践-实现一个简单的数据持久化类(上)
- swift学习——关于断言
- swift学习笔记之-枚举
- Swift学习之枚举
- Swift学习笔记 - 位移枚举的按位或运算