首页 > 代码库 > swift 简介和常量与变量 --- swift 入门

swift 简介和常量与变量 --- swift 入门

一、思维导图技术分享

二、

技术分享代码

//创建UIView 和按钮     
let views = UIView(frame: CGRect(x:20, y: 20, width: 100, height: 100))//FIXME:改变viewsframe
        views.backgroundColor = UIColor.yellow
        view.addSubview(views)
        
        //MARK: 创建一个按钮
        let Button2 = UIButton(type: .contactAdd)
        Button2.backgroundColor = UIColor.red  //FIXME:sfsl
        Button2.frame = views.bounds
        views.addSubview(Button2)
        
        //        Button2 .addTarget(self, action: #selector(button2Click), for:)
        Button2.addTarget(self, action: #selector(button2Click), for: .touchUpInside)
    }
    //MARK:-- methods response
    func button2Click()  {
        print("按钮单击")
    }
//常量和变量
1
let a = 10 2 let b:Double = 105.3 3 // Binary operator ‘+‘ cannot be applied to operands of type ‘Int‘ and ‘Double‘ 4 print(a+Int(b)) 5 6 7 var name:String = "Hello Word" //不管是变量还是常量必须初始化,且常量的值不能改变 8 name = "我是 Hello Word" 9 print(name)
 //分支和三运算
 //1 if f
        let a = 10
        
        if a > 5 {
            print("a>5")
        }else{
            print("a<5")
        }
        //2 三目
        
//        a > 5 ? print("a大") : print("a小")
        //或写成
        a > 5 ? print("a大大大") :()

 

swift 简介和常量与变量 --- swift 入门