关于Swift 动态生成按钮

//
//  FirstViewController.swift
//  esb
//
//  Created by jiangzhihao on 2018/11/12.
//  Copyright © 2018年 jiangzhihao. All rights reserved.
//

import UIKit

class FirstViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        for i in 0 ..< 3  {
            let btnLogin = UIButton(type: .roundedRect)
            btnLogin.frame = CGRect(x: 10 + i * 125, y: 200, width: 120, height: 50)
            btnLogin.setTitle("按钮演示", for: .normal)
            btnLogin.setTitleColor(.black, for: .normal)
            btnLogin.titleLabel?.font = UIFont.systemFont(ofSize: 18)
            btnLogin.layer.cornerRadius = 5
            btnLogin.layer.borderWidth = 1
            btnLogin.layer.borderColor = UIColor.black.cgColor
            btnLogin.tag = i
            btnLogin.addTarget(self, action: #selector(tapped_login(_:)), for: .touchUpInside)
            self.view.addSubview(btnLogin)
        }
    }

    @objc func tapped_login(_ button:UIButton) {
        print("ok:\(button.tag)")
    }

}
阅读全文

关于Swift 泛型

// 泛型
// 定义一个交换两个变量的函数
func swap<T>(_ a: inout T, _ b: inout T) {
    let temp = a
    a = b
    b = temp
}

var numb1 = 77
var numb2 = 33

print("交换前数据: \(numb1) 和 \(numb2)")
swap(&numb1, &numb2)
print("交换后数据: \(numb1) 和 \(numb2)")

var str1 = "jiang"
var str2 = "zhihao"

print("交换前数据: \(str1) 和 \(str2)")
swap(&str1, &str2)
print("交换后数据: \(str1) 和 \(str2)")

关于Swift if for while case

        let value = 20
        
        if (value > 20)
        {
            print("大于20")
        }
        else if (value < 20)
        {
            print("小于20")
        }
        else
        {
            print("等于20")
        }
        
        let switchCondition:String = "jzh"
        switch switchCondition
        {
        case "jzh":
            print("ok jzh");
            fallthrough
        case "jiang":
            print("ok jiang");
            fallthrough
        default:
            print("ok idodo")
            
        }
        
        let price = 100
        
        switch price
        {
        case 0:
            print("免费")
        case 10..<50:
            print("超低价")
        case 50..<100:
            print("低价")
        case 100...500:
            print("价格适中")
        case 501...999_999_999:
            print("贵")
        default:
            print("非常贵")
        }
        
        for index in 1...5
        {
            print("\(index)")
        }
        
        let names = ["a", "b", "c", "d"]
        for name in names
        {
            print("Hello, \(name)!")
        }
        
        var index = 10
        
        while index < 20
        {
            print( "index 的值为 \(index)")
            index = index + 1
        }

关于Swift 定义函数 返回多个值

// 定义函数,返回值类型是String
func getUser(name: String, age:Int) -> (String)
{
    return "name:" + name + " age:" + "\(age)"
}

// 调用getUser函数
let user = getUser(name: "jzh", age: 35)
print("user:\(user)")
// 调用getUser函数
let user = getUser(name: "jzh", age: 35)
print("user:\(user)")

// 定义一个可以返回多个值的函数(返回两个值:name和age)
func getUserInfo() -> (name:String, age:Int)
{
    return ("jzh", 35)
}

print(getUserInfo())
print("name: \(getUserInfo().name)")
print("age: \(getUserInfo().age)")