swift中的block

protocol StopTimer { func stopTimer(); } typealias InputClosureType = (String) -> Void class ViewController: UIViewController,StopTimer { var button:UIButton? = nil; var timer:Timer? = nil typealias FuncTest = (Int)->Int; var number:NSInteger = 60; var stop:StopTimer? = nil; func kj(_ name:String){} var m:((_ custom:String)->String)? // print("--=-=-=-=-=-",KM); var ms:((_ name:String)->Int)?; // var m :FuncTest?=nil; // var m = FuncTest; var testBlock:((_ kl:String)->(Int))?; // typealias funBlock = (_ number:Int)->Int; @IBOutlet weak var animateLabel: UILabel! func names(_ mg:(Int)){ print("block====%@",mg); }; var callBack = names; override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view,typically from a nib. button = UIButton(frame: CGRect(x: 20,y: 40,width: 150,height: 200)); button?.backgroundColor=UIColor.blue; button?.setTitle("60",for: UIControlState.normal) button?.addTarget(self,action: #selector(buttonClick(sender:)),for: UIControlEvents.touchUpInside) self.view.addSubview(button!) stop = self; // self.kj { (m) -> Int in // // // print("执行完成了") // // return 2; // } var m = ms; let names = ["chris","alex","bkljk "]; func sortFunc(numbers:String,numberw:String)->Bool { return numbers>numberw; } var sortm = names.sorted(by: sortFunc); print("刷选出来的结果是",sortm); print("===m",m); // self.kj { (name) -> Int in // // // // print("name的值为====>%@\n",name); // // // // // // return 2; // // }; // callBack = {(msgk) in Int // // // // // return 20; // // }; testBlock = { (kkjj:String) -> Int in print("结果是lklanxm===%@",kkjj) return 50; } let result = testBlock!("新的发现哦"); print("输出结果是",result) } func buttonClick(sender:UIButton) { if timer==nil { let weak = self; timer = Timer.scheduledTimer(withTimeInterval: 1,repeats: true,block: { (t) in self.number -= 1; self.button?.setTitle(("\(self.number)"),for: UIControlState.normal); if self.number == 0 { self.timer?.invalidate() self.timer=nil; // if self.StopTimer != nil{ self.stop?.stopTimer(); // } // if (.m != nil) // { //// self.m = {( "hah") in Void //// //// return 20; //// } // } } }) RunLoop.current.add(timer!,forMode: RunLoopMode.commonModes) timer?.fire() } } func stopTimer() { print("停止操作了"); } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // dispose of any resources that can be recreated. } }

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...