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 }