• Swift


    struct CenterPoint {
        var x = 0,y = 0
    }
    func + (left:CenterPoint,right:CenterPoint) -> CenterPoint {
        return CenterPoint.init(x: left.x + right.x, y: left.y + right.y)
    }
    func == (left:CenterPoint,right:CenterPoint) -> Bool {
        return (left.x == right.x) && (left.y == right.y)
    }
    func != (left:CenterPoint,right:CenterPoint) -> Bool {
        return !(left == right)
    }
    func += (left:inout CenterPoint,right:CenterPoint){
         left = left + right
        print(left)
    }
    class AreaViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            self.view.backgroundColor = UIColor.white
            //1.0,闭区间运算符:a...b
            for icount in 512...1000 {
                print(icount)
            }
            //1.2,半闭区间运算符:a..<b
            var fruits = ["apple","orange","banana"]
            for i in 0..<fruits.count {
                print("i---(fruits[i])")
            }
            fruits.append("Longan")
            
            //1.3,倒序循环
            for j in (0..<10).reversed() {
                print(j)
            }
            //1.4,字符串范围也可以使用区间运算符
            let words = "mujin.com"
            let index = words.index(words.startIndex, offsetBy: 4)
            let index2 = words.index(words.startIndex, offsetBy: 6)
            let range1 = Range(uncheckedBounds: (lower: index, upper: index2))
            let rangeStr1 = String(words[range1]) //ge
            print(rangeStr1)
            //使用区间运算符
            let range2 = words.index(words.startIndex, offsetBy: 4)..<words.index(words.startIndex, offsetBy: 6)
            let rangeStr2 = String(words[range2])
            print(rangeStr2)
            
            //1.5,区间运算符除了返回一个Range外,还可以接受Comparable的输入,返回ClosedInterval或HalfOpenInterval
            let interval = "a"..."z"
            for c in words {
                if !interval.contains(String(c)){
                    print("(c)不是小写字母")
                }
            }
            //通过 + 进行方法重载相加
            var point1 = CenterPoint.init(x: 2, y: 3)
            let point2 = CenterPoint.init(x: 4, y: 5)
            let point3 = point1 + point2
            print(point3)
            print(point1 == point2)
            print(point1 != point2)
            point1 += point2
        }
  • 相关阅读:
    定时器的应用---查询方式---让8个LED灯,左右各4个来回亮
    单片机实现60s定时器
    单片机不同晶振怎么计算延迟时间?
    573锁存器驱动8段数码管
    51单片机英文引脚等中文对照
    Java【小考】
    viso2010从mysql中导出ER图
    驱动继电器实验
    驱动蜂鸣器的实验
    驱动数码管的实验
  • 原文地址:https://www.cnblogs.com/edensyd/p/9405994.html
Copyright © 2020-2023  润新知