myClour0!(10,50)
//回调:原理和OC中一样的!都是通过参数回调!!!
VC1:
@IBOutlet weak var textL: UILabel!
@IBAction func nextClick(sender: AnyObject) {
let secondVC = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle()).instantiateViewControllerWithIdentifier("OtherViewController")as!OtherViewController
secondVC.setBackMyClosure { (inputStr:String) -> Void in
self.textL.text = inputStr
}
self.presentViewController(secondVC, animated: true, completion: nil)
}
VC2:
@IBOutlet weak var textL: UILabel!
@IBAction func nextClick(sender: AnyObject) {
let secondVC = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle()).instantiateViewControllerWithIdentifier("OtherViewController")as!OtherViewController
secondVC.setBackMyClosure { (inputStr:String) -> Void in
self.textL.text = inputStr
}
self.presentViewController(secondVC, animated: true, completion: nil)
}
在Swift的数组中自带了一些比较好用的闭包函数,例如Map, Filter, Reduce。接下来就好好的看一下这些闭包,用起来还是比较爽的。
map:map闭包函数的功能就是对数组中的每一项进行遍历,然后通过映射规则对数组中的每一项进行处理,最终的返回结果是处理后的数组(以一个新的数组形式出现)。当然,原来数组中的元素值是保持不变的,这就是map闭包函数的用法与功能。
Filter (过滤器):而在数组中的Filter用来过滤数组中的数据,并且返回新的数组,新的数组中存放的就是符合条件的数据
Reduce : 在swift的数组中使用Reduce闭包函数来合并items, 并且合并后的Value
*/
let family = [1,2,3,4]
var familyMap = family.map { (item:Int) -> String in
return "我是老\(item)"
}
family
familyMap
let heightOfPerson = [170,180,190,160,168,132,159]
let heightOfPersonFilter = heightOfPerson.filter { (height:Int) -> Bool in
return height >= 180
}
heightOfPerson
heightOfPersonFilter
print((heightOfPersonFilter as NSArray).objectAtIndex(0))
let sakary = [1000,2000,3000,4000,5000,6000,7000,8000,9000]
let sumSalary = sakary.reduce(0) { (sumTemp:Int, salay:Int) -> Int in
return sumTemp + salay
}