├── Easy-Cal.swift ├── LICENSE └── README.md /Easy-Cal.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Easy-Cal.swift 3 | // tableview 4 | // 5 | // Created by 王 巍 (@onevcat) on 14-6-4. 6 | // Copyright (c) 2014年 OneV's Den. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import CoreGraphics 11 | 12 | func +(lhs: Int, rhs: Double) -> Double { 13 | return Double(lhs) + rhs 14 | } 15 | 16 | func +(lhs: Double, rhs: Int) -> Double { 17 | return lhs + Double(rhs) 18 | } 19 | 20 | func +(lhs: Int, rhs: Float) -> Float { 21 | return Float(lhs) + rhs 22 | } 23 | 24 | func +(lhs: Float, rhs: Int) -> Float { 25 | return lhs + Float(rhs) 26 | } 27 | 28 | func +(lhs: Float, rhs: Double) -> Double { 29 | return Double(lhs) + rhs 30 | } 31 | 32 | func +(lhs: Double, rhs: Float) -> Double { 33 | return lhs + Double(rhs) 34 | } 35 | 36 | func +(lhs: UInt, rhs: Double) -> Double { 37 | return Double(lhs) + rhs 38 | } 39 | 40 | func +(lhs: Double, rhs: UInt) -> Double { 41 | return lhs + Double(rhs) 42 | } 43 | 44 | func +(lhs: UInt, rhs: Float) -> Float { 45 | return Float(lhs) + rhs 46 | } 47 | 48 | func +(lhs: Float, rhs: UInt) -> Float { 49 | return lhs + Float(rhs) 50 | } 51 | 52 | func +(lhs: UInt, rhs: Int) -> Int { 53 | return Int(lhs) + rhs 54 | } 55 | 56 | func +(lhs: Int, rhs: UInt) -> Int { 57 | return lhs + Int(rhs) 58 | } 59 | 60 | func +(lhs: Int64, rhs: UInt64) -> Int64 { 61 | return lhs + Int64(rhs) 62 | } 63 | 64 | func +(lhs: UInt64, rhs: Int64) -> Int64 { 65 | return Int64(lhs) + rhs 66 | } 67 | 68 | func -(lhs: Int, rhs: Double) -> Double { 69 | return Double(lhs) - rhs 70 | } 71 | 72 | func -(lhs: Double, rhs: Int) -> Double { 73 | return lhs - Double(rhs) 74 | } 75 | 76 | func -(lhs: Int, rhs: Float) -> Float { 77 | return Float(lhs) - rhs 78 | } 79 | 80 | func -(lhs: Float, rhs: Int) -> Float { 81 | return lhs - Float(rhs) 82 | } 83 | 84 | func -(lhs: Float, rhs: Double) -> Double { 85 | return Double(lhs) - rhs 86 | } 87 | 88 | func -(lhs: Double, rhs: Float) -> Double { 89 | return lhs - Double(rhs) 90 | } 91 | 92 | func -(lhs: UInt, rhs: Double) -> Double { 93 | return Double(lhs) - rhs 94 | } 95 | 96 | func -(lhs: Double, rhs: UInt) -> Double { 97 | return lhs - Double(rhs) 98 | } 99 | 100 | func -(lhs: UInt, rhs: Float) -> Float { 101 | return Float(lhs) - rhs 102 | } 103 | 104 | func -(lhs: Float, rhs: UInt) -> Float { 105 | return lhs - Float(rhs) 106 | } 107 | 108 | func -(lhs: UInt, rhs: Int) -> Int { 109 | return Int(lhs) - rhs 110 | } 111 | 112 | func -(lhs: Int, rhs: UInt) -> Int { 113 | return lhs - Int(rhs) 114 | } 115 | 116 | func -(lhs: Int64, rhs: UInt64) -> Int64 { 117 | return lhs - Int64(rhs) 118 | } 119 | 120 | func -(lhs: UInt64, rhs: Int64) -> Int64 { 121 | return Int64(lhs) - rhs 122 | } 123 | 124 | func *(lhs: Int, rhs: Double) -> Double { 125 | return Double(lhs) * rhs 126 | } 127 | 128 | func *(lhs: Double, rhs: Int) -> Double { 129 | return lhs * Double(rhs) 130 | } 131 | 132 | func *(lhs: Int, rhs: Float) -> Float { 133 | return Float(lhs) * rhs 134 | } 135 | 136 | func *(lhs: Float, rhs: Int) -> Float { 137 | return lhs * Float(rhs) 138 | } 139 | 140 | func *(lhs: Float, rhs: Double) -> Double { 141 | return Double(lhs) * rhs 142 | } 143 | 144 | func *(lhs: Double, rhs: Float) -> Double { 145 | return lhs * Double(rhs) 146 | } 147 | 148 | func *(lhs: UInt, rhs: Double) -> Double { 149 | return Double(lhs) * rhs 150 | } 151 | 152 | func *(lhs: Double, rhs: UInt) -> Double { 153 | return lhs * Double(rhs) 154 | } 155 | 156 | func *(lhs: UInt, rhs: Float) -> Float { 157 | return Float(lhs) * rhs 158 | } 159 | 160 | func *(lhs: Float, rhs: UInt) -> Float { 161 | return lhs * Float(rhs) 162 | } 163 | 164 | func *(lhs: UInt, rhs: Int) -> Int { 165 | return Int(lhs) * rhs 166 | } 167 | 168 | func *(lhs: Int, rhs: UInt) -> Int { 169 | return lhs * Int(rhs) 170 | } 171 | 172 | func *(lhs: Int64, rhs: UInt64) -> Int64 { 173 | return lhs * Int64(rhs) 174 | } 175 | 176 | func *(lhs: UInt64, rhs: Int64) -> Int64 { 177 | return Int64(lhs) * rhs 178 | } 179 | 180 | func /(lhs: Int, rhs: Double) -> Double { 181 | return Double(lhs) / rhs 182 | } 183 | 184 | func /(lhs: Double, rhs: Int) -> Double { 185 | return lhs / Double(rhs) 186 | } 187 | 188 | func /(lhs: Int, rhs: Float) -> Float { 189 | return Float(lhs) / rhs 190 | } 191 | 192 | func /(lhs: Float, rhs: Int) -> Float { 193 | return lhs / Float(rhs) 194 | } 195 | 196 | func /(lhs: Float, rhs: Double) -> Double { 197 | return Double(lhs) / rhs 198 | } 199 | 200 | func /(lhs: Double, rhs: Float) -> Double { 201 | return lhs / Double(rhs) 202 | } 203 | 204 | func /(lhs: UInt, rhs: Double) -> Double { 205 | return Double(lhs) / rhs 206 | } 207 | 208 | func /(lhs: Double, rhs: UInt) -> Double { 209 | return lhs / Double(rhs) 210 | } 211 | 212 | func /(lhs: UInt, rhs: Float) -> Float { 213 | return Float(lhs) / rhs 214 | } 215 | 216 | func /(lhs: Float, rhs: UInt) -> Float { 217 | return lhs / Float(rhs) 218 | } 219 | 220 | func /(lhs: UInt, rhs: Int) -> Int { 221 | return Int(lhs) / rhs 222 | } 223 | 224 | func /(lhs: Int, rhs: UInt) -> Int { 225 | return lhs / Int(rhs) 226 | } 227 | 228 | func /(lhs: Int64, rhs: UInt64) -> Int64 { 229 | return lhs / Int64(rhs) 230 | } 231 | 232 | func /(lhs: UInt64, rhs: Int64) -> Int64 { 233 | return Int64(lhs) / rhs 234 | } 235 | 236 | // MARK: - Core Graphics Calculation 237 | 238 | func +(lhs: CGFloat, rhs: Float) -> CGFloat { 239 | return lhs + CGFloat(rhs) 240 | } 241 | 242 | func +(lhs: Float, rhs: CGFloat) -> CGFloat { 243 | return CGFloat(lhs) + rhs 244 | } 245 | 246 | func +(lhs: CGFloat, rhs: Double) -> CGFloat { 247 | return lhs + CGFloat(rhs) 248 | } 249 | 250 | func +(lhs: Double, rhs: CGFloat) -> CGFloat { 251 | return CGFloat(lhs) + rhs 252 | } 253 | 254 | func +(lhs: CGFloat, rhs: Int) -> CGFloat { 255 | return lhs + CGFloat(rhs) 256 | } 257 | 258 | func +(lhs: Int, rhs: CGFloat) -> CGFloat { 259 | return CGFloat(lhs) + rhs 260 | } 261 | 262 | func +(lhs: CGFloat, rhs: UInt) -> CGFloat { 263 | return lhs + CGFloat(rhs) 264 | } 265 | 266 | func +(lhs: UInt, rhs: CGFloat) -> CGFloat { 267 | return CGFloat(lhs) + rhs 268 | } 269 | 270 | func -(lhs: CGFloat, rhs: Float) -> CGFloat { 271 | return lhs - CGFloat(rhs) 272 | } 273 | 274 | func -(lhs: Float, rhs: CGFloat) -> CGFloat { 275 | return CGFloat(lhs) - rhs 276 | } 277 | 278 | func -(lhs: CGFloat, rhs: Double) -> CGFloat { 279 | return lhs - CGFloat(rhs) 280 | } 281 | 282 | func -(lhs: Double, rhs: CGFloat) -> CGFloat { 283 | return CGFloat(lhs) - rhs 284 | } 285 | 286 | func -(lhs: CGFloat, rhs: Int) -> CGFloat { 287 | return lhs - CGFloat(rhs) 288 | } 289 | 290 | func -(lhs: Int, rhs: CGFloat) -> CGFloat { 291 | return CGFloat(lhs) - rhs 292 | } 293 | 294 | func -(lhs: CGFloat, rhs: UInt) -> CGFloat { 295 | return lhs - CGFloat(rhs) 296 | } 297 | 298 | func -(lhs: UInt, rhs: CGFloat) -> CGFloat { 299 | return CGFloat(lhs) - rhs 300 | } 301 | 302 | func *(lhs: CGFloat, rhs: Float) -> CGFloat { 303 | return lhs * CGFloat(rhs) 304 | } 305 | 306 | func *(lhs: Float, rhs: CGFloat) -> CGFloat { 307 | return CGFloat(lhs) * rhs 308 | } 309 | 310 | func *(lhs: CGFloat, rhs: Double) -> CGFloat { 311 | return lhs * CGFloat(rhs) 312 | } 313 | 314 | func *(lhs: Double, rhs: CGFloat) -> CGFloat { 315 | return CGFloat(lhs) * rhs 316 | } 317 | 318 | func *(lhs: CGFloat, rhs: Int) -> CGFloat { 319 | return lhs * CGFloat(rhs) 320 | } 321 | 322 | func *(lhs: Int, rhs: CGFloat) -> CGFloat { 323 | return CGFloat(lhs) * rhs 324 | } 325 | 326 | func *(lhs: CGFloat, rhs: UInt) -> CGFloat { 327 | return lhs * CGFloat(rhs) 328 | } 329 | 330 | func *(lhs: UInt, rhs: CGFloat) -> CGFloat { 331 | return CGFloat(lhs) * rhs 332 | } 333 | 334 | func /(lhs: CGFloat, rhs: Float) -> CGFloat { 335 | return lhs / CGFloat(rhs) 336 | } 337 | 338 | func /(lhs: Float, rhs: CGFloat) -> CGFloat { 339 | return CGFloat(lhs) / rhs 340 | } 341 | 342 | func /(lhs: CGFloat, rhs: Double) -> CGFloat { 343 | return lhs / CGFloat(rhs) 344 | } 345 | 346 | func /(lhs: Double, rhs: CGFloat) -> CGFloat { 347 | return CGFloat(lhs) / rhs 348 | } 349 | 350 | func /(lhs: CGFloat, rhs: Int) -> CGFloat { 351 | return lhs / CGFloat(rhs) 352 | } 353 | 354 | func /(lhs: Int, rhs: CGFloat) -> CGFloat { 355 | return CGFloat(lhs) / rhs 356 | } 357 | 358 | func /(lhs: CGFloat, rhs: UInt) -> CGFloat { 359 | return lhs / CGFloat(rhs) 360 | } 361 | 362 | func /(lhs: UInt, rhs: CGFloat) -> CGFloat { 363 | return CGFloat(lhs) / rhs 364 | } 365 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Wei Wang 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Easy-Cal-Swift 2 | ============== 3 | 4 | ### Overview 5 | 6 | This file is an overloading of +-*/ operator for Swift, to make it easier to use (and not so strict) 7 | 8 | It can make your life with Swift easier when doing calculation between different type. Swift has a type check and requires left operand and the right one the same type. It causes a lot of trouble that we must convert the type every time. 9 | 10 | ``` 11 | var a = 3 12 | var b = 2.0 13 | a + b //Compile error 14 | a - b //Oops, error again 15 | a * b //Wtf.. 16 | a / b //God please save me 17 | 18 | //You have to write these instead 19 | Double(a) + b 20 | Double(a) - b 21 | Double(a) * b 22 | Double(a) / b 23 | ``` 24 | 25 | Yes, it is type safe, but it is also time wasted. 26 | 27 | ### Usage 28 | 29 | Just put the `Easy-Cal.swift` to your project, and you can now have a traditional C-like implicit conversion between `Int`, `Float` and `Double`. So you can write as you would expect without annoying error messages. 30 | 31 | ``` 32 | var a = 3 33 | var b = 2.0 34 | a + b //5.0 35 | a - b //1.0 36 | a * b //6.0 37 | a / b //1.5 38 | ``` 39 | 40 | `UInt` is also implemented, but it seems there is a bug in this beta seed PlayGround, in which there is a fatal error when you assign an `UInt` value :(. Fortunately, the `UInt` works well in project. 41 | 42 | ### Risk 43 | Yes, you lose type safe when using these operators, which is opposite to Swift design. But I find at almost all the time, you will know what you are doing with these numbers and the conversion will be just exactly what you want. 44 | --------------------------------------------------------------------------------