闭包
let cs = {
(a: Int, b:Int) in
return a + b
}//创建一个闭包,参数类型要么前面说明,要么在里面创说明
let ss = cs (1,2)//调用,进行传值
print(ss)
func es (a:Int, b :Int, c:(Int,Int)->Int) ->Int{
return c(a,b)
}//创建一个有闭包参数的函数
let pp = es(11, b: 34, c: cs)
print(pp)
var arr1 = [1,3,2,5]
var arr2 = arr1
.map({$0*$0})//将数组中的元素进行平方
.filter({$0>3})//筛选arr1大于3
.map({"\($0)"})//将数组元素转换为字符串类型
.reduce("", combine: {$0+$1})//将字符串连接起来
print(arr2)
let kk:(q:Int, w:Int)->Int = {
q,w in
return q+w
}
let ww = kk(q: 1,w: 2)//对闭包进行传值
print(ww)
let uu = {
() -> Int in
return 33
}() //执行闭包
print(uu)
func test1(a:Int) -> (Int)->(Int)->Int{
var b=3
return {
print("二层",$0,a,b)
//return $0+a+b
return{
print("三层",$0,a,b)
return $0+a+b
}
}
}
let ss2 = test1(33)(3)(44)//三层调用闭包
print(ss2)
用代码实现之前的猜数字的功能和界面
import UIKit
class ViewController: UIViewController {
var result = arc4random_uniform(100)//生成一个100以内的随机数
let label1 = UILabel()
let textfield = UITextField()
override func viewDidLoad() {
super.viewDidLoad()
//史上最强猜数字框
let label = UILabel()
label.frame = CGRectMake(60, 30, 200, 50)
label.backgroundColor = UIColor.brownColor()
label.text = "史上最强猜数字"
label.textAlignment = .Center//文本框内容字对齐
self.view.addSubview(label)
//结果输出框
label1.frame = CGRectMake(60, 300, 200, 50)
label1.text = "结果为:"
label1.backgroundColor = UIColor.blueColor()
label1.textAlignment = .Center
self.view.addSubview(label1)
//数字0
let label2 = UILabel()
label2.frame = CGRectMake(90, 90, 30, 50)
label2.text = "0"
label2.backgroundColor = UIColor.greenColor()
label2.textAlignment = .Center
self.view.addSubview(label2)
//数字100
let label3 = UILabel()
label3.frame = CGRectMake(190, 90, 30, 50)
label3.text = "100"
label3.backgroundColor = UIColor.greenColor()
label3.textAlignment = .Center
self.view.addSubview(label3)
//到
let label4 = UILabel()
label4.frame = CGRectMake(140, 90, 30, 50)
label4.text = "到"
label4.backgroundColor = UIColor.cyanColor()
label4.textAlignment = .Center
self.view.addSubview(label4)
//输入文本框
textfield.frame = CGRectMake(60, 150, 200, 50)
textfield.backgroundColor = UIColor.redColor()
textfield.keyboardType = UIKeyboardType.NumbersAndPunctuation//设置键盘类型
textfield.borderStyle = .RoundedRect//设置圆边框
textfield.placeholder = "请输入内容"
textfield.clearsOnBeginEditing = true//清除上一次的输入
textfield.clearButtonMode = .WhileEditing//设置输入框的清除按钮
textfield.font = UIFont.boldSystemFontOfSize(30)//设置字体大小
self.view.addSubview(textfield)
//确定按钮
let but = UIButton(type:.System)
but.frame = CGRectMake(110, 220, 100, 50)
but.backgroundColor = UIColor.greenColor()
but.setTitle("确定", forState: .Normal)
but.setTitle("选中", forState: .Selected)
//let image = UIImage(named: "check")
//let image1 = UIImage(named: "check1")
//but.setBackgroundImage(image, forState: .Normal)
//but.setBackgroundImage(image1, forState: .Selected)
self.view.addSubview(but)
//添加触碰事件动作
but.addTarget(self, action: #selector(DidClick), forControlEvents: .TouchUpInside)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func DidClick(){//定义点击调用的函数
let guess = UInt32(textfield.text!)
print("猜测数字为",textfield.text)
print("实际数字是:",result)
if guess == result {
label1.text = "猜对了"
}
else if guess > result{
label1.text = "不好意思,数字大了"
}else {
label1.text = "不好意思,数字小了"
}
}
//设置点击屏幕空白处,发生第一响应,撤销键盘
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.view.endEditing(true)
}
}