关于Swift的闭包,尾随闭包,Swift 中类型检测使用关键字is,类型转换使用关键字as。Any类,和AnyClass和字典,字符串的方法,直接copy自己写的demo!注释比较详细,有兴趣学习的可以直接copy!
import UIKit
//block 定义,block 是一种数据类型在OC里面,这里也是一样,名字 BlockTarget(参数)(back)
typealias BlockTarget = (String)->(Void)
typealias Block = ()->(Void)
class ViewController: UIViewController {
var blockTarget:BlockTarget?
var blockAction:((String) -> (Void))?
// var blockclick = (()->(Void)).self
override func viewDidLoad() {
super.viewDidLoad()
var but:UIButton = UIButton.init(frame: CGRect.init(x: 0, y: 200, width: 100, height: 200))
but.backgroundColor = UIColor.red
but.addTarget(self, action: #selector(blockself), for: UIControlEvents.touchUpInside)
self.view.addSubview(but)
/*
*-----以下都是接触比较少的语法对于纯IOS程序员来说----
*-----------我会尽量我把知道的注释详细的-----------
*/
for index in 1...5{
print("index = \(index)")
//从1到5 循环
}
//这是一个字典
let dic = ["string":1.2,"ant":2.2,"cat":3.2]
for (j,z)in dic{
//系统自动找出键值对。注意:但是定义的时候让系统知道数据类型,键值对要一致!不一致会被设成any类型,这个类型后面我慢慢解刨
print(z)
}
//要定明白数据类型,如果是其他的数据类型的话就要定义any类型
// let dic1 = ["s":3,"s":"2.2","f":2.2]
// for (k,l)in dic1{
//
//
//
// }
// let dic2:Any = ["s":3,"s":"2.2","f":2.2]
//
// for (a,v) in dic2{
//
// print("a = (a)")
//
// }
/**
*1.两个泛型类型,在数组和字典里面怎么应用?
*2.元祖是否就是数组?
*3.
*/
// 可变的类型,(numbers:N...)可变参数,可变参数可以接受零个或多个值,局部函数
func ShowNumber(numbers:N...){
for i in numbers{
print(i)
}
}
ShowNumber(numbers: 1,2,3,4,5,6,7,8,9,10)
func showmtable(number:S)->Int{
//如果指定了,数据类型就要强转下
return number as!Int
}
let b = showmtable(number: 1)
print("b = \(b)")
//MARK
/*
闭包的语法格式
1 以下定义了一个接收参数并返回指定类型的闭包语法:
{
(parameters) -> return type in
statements
}
2 尾随闭包
(parameters) -> return type in {
statements
}
*/
//fun studname()->() --- { (parameters)->return type in statements }
let Studname = {
()->() in print("swift 闭包")
}
Studname()
let studname2 = {
print("Swift 尾随闭包")
}
studname2()
//闭包形式接受两个参数并返回布尔值
let divide = {
(val1:Int,val2:Int)-> Int in
let Value = val1/val2
return Value
}
let result = divide(10,20)
print(result)
//Sort 函数
//Swift 一建搞定
let Name = ["AH","AS","D","S","BE"]
let reversed = Name.sorted()
print(reversed)//直接返回一个数组
// 尾随闭包,即{}部分放在()之后。不知道是有啥作用
// 捕获值,函数里面嵌套函数,返回的是一个方法
func makeINcrementor(forIncrement Amount:Int)->()->Int{
var RuningTotal = 0
func incrermentor()->Int{
RuningTotal += Amount
print("runingTotal = \(RuningTotal)")
return RuningTotal
}
return incrermentor
}
let rescrrsed05 = makeINcrementor(forIncrement: 10)
print(rescrrsed05)
//打印是(Function),返回一个方法给你。。!
//block 的应用,
/*步骤
*1.定义block类型!typealias BlockTarget = (String)->(Void)
*((String) -> (Void))?
*2.现在方法 {ss:string}->() in
*3.调用block("字符串")
*/
self.blockAction = {
(ss:String)->() in
print(ss)
}
class Marks{
var Chinese:String
init(CHinese:String) {
self.Chinese = CHinese;
}
}
class grageOne:Marks{
var Maths:String
init(chainese:String,maths:String) {
self.Maths = maths
super.init(CHinese: chainese)
}
}
class GraheTwo:Marks{
var english:String
init(chinese:String,englich:String) {
self.english = englich
super.init(CHinese: chinese)
}
}
// Swift 中类型检测使用关键字is,类型转换使用关键字as。
let studenMark = [grageOne.init(chainese: "89", maths: "99")
,GraheTwo.init(chinese: "wudi", englich: "haha")]
if studenMark is GraheTwo{
print("二年级的成绩")
}else{
print("不是二年级的成绩")
}
for item in studenMark
{
if let xiaoWangMark = item as? grageOne
{
print(xiaoWangMark.Chinese)
}
else if let xiaoZhangMark = item as? GraheTwo
{
print(xiaoZhangMark.Chinese)
}
}
/*
Any和AnyObject的类型转换
Swift为不确定类型提供了两种特殊类型别名:
1 AnyObject可以代表任何class类型的实例。
2 Any可以表示任何类型,包括方法类型(function types)。
注意:只有当你明确的需要它的行为和功能时才使用Any和AnyObject。
*/
var arrayany01 = [Any]()
print(arrayany01)
arrayany01.append(12)
arrayany01.append(3.14159)
arrayany01.append("Any 实例")
arrayany01.append(grageOne.init(chainese: "hehe", maths: "99"))
arrayany01.append(GraheTwo.init(chinese: "haha", englich: "88"))
print(arrayany01)
for item in arrayany01{
if let itemInt = item as? Int{
print("整形为\(itemInt)")
}
}
func Buttonclock() -> Void {
print("点击")
}
print(Buttonclock())
let arrayAnyObjct1:Array = [grageOne.init(chainese: "wudi", maths: "yeshi"),GraheTwo.init(chinese: "78", englich: "92",0), "DevZhang", 30]
let arrayAnyObject01:Array = [grageOne(chainese: "86", maths: "66"), GraheTwo(chinese: "78", englich: "92"), "DevZhang" as AnyObject, 30 as AnyObject]
print(arrayAnyObject01)
let arrayAnyObject02:Array = [grageOne(chainese: "86", maths: "66"), GraheTwo(chinese: "78", englich: "92"), "DevZhang" as AnyObject, 30 as AnyObject, Buttonclock()]
print(arrayAnyObject02)
}
//这个是类函数,我就不明白了 为什么事件 必须加@objc 要不然没有用。。!报错!
@objc func blockself (){
self.blockAction!("fake")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func touchesBegan(_ touches: Set, with event: UIEvent?) {
}
}