IOS: learning notes, Swift operator Definition
You can customize the Swift operator by adding a simple identifier.
@ Infix: center operation. For example, +,-, *,/operation.
@ Prefix: prefix. Example-
@ Postfix post operation. a ++, --
@ Assignment value assignment operation. + =,-=, -- a, ++
//// Main. swift // SwiftBasic /// Created by yao_yu on 14-7-27. // Copyright (c) 2014 yao_yu. all rights reserved. // import Foundationstruct Vector2D {var x = 0.0, y = 0.0} @ infix func + (a: Vector2D, B: Vector2D)-> Vector2D {return Vector2D (x:. x + B. x, y:. y + B. y)} @ infix func-(a: Vector2D, B: Vector2D)-> Vector2D {return a +-B} @ prefix func-(a: Vector2D) -> Vector2D {return Vector2D (x:-. x, y:-. Y)} @ assignment func + = (inout a: Vector2D, B: Vector2D) {a = a + B} @ prefix @ assignment func ++ (inout a: Vector2D) {++. x ++. y} @ postfix func ++ (a: Vector2D)-> Vector2D {return a + Vector2D (x: 1, y: 1)} @ infix func = (a: Vector2D, b: Vector2D)-> Bool {return (. x = B. x) & (. y = B. y)} @ infix func! = (A: Vector2D, B: Vector2D)-> Bool {return! (A = B)} func Vector2D_Test () {var a = Vector2D (x: 1, y: 2), B = Vector2D (x: 3, y: 5) var c = a + B a + = B println ("(\ (c. x), \ (c. y) ") println (" (\ (. x), \ (. y) ") assert (a = c," a = B failed ") a + = B assert (! = C, "! = B failed ") c = a ++ println (" (\ (. x), \ (. y) ") println (" (\ (c. x), \ (c. y) ")} Vector2D_Test ()