IOS: Learning notes, swift operator definitions

Source: Internet
Author: User

The swift operator can be defined by itself, simply by adding a simple marker.

@infix the middle operation. such as +,-, *,/operations

@prefix the predecessor operation. such as

@postfix the post-operation. a++, a--

@assignment assignment operation. + =,-=,--a, ++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)-&gt ;  vector2d{return vector2d (x:a.x + b.x, Y:A.Y + b.y)} @infix func-(a:vector2d, b:vector2d), vector2d{return a +-B} @prefix func-(a:vector2d)-vector2d{return vector2d (x:-a.x, Y:-a.y)} @assignment func + = (inout a:vecto R2D, b:vector2d) {a = a + b} @prefix @assignment func + + (inout a:vector2d) {++a.x ++a.y} @postfix func + + (a:vector 2D)-vector2d{return a + vector2d (x:1, Y:1)} @infix func = = (a:vector2d, b:vector2d), bool{return (a.x = = b.x) && (a.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 prin TLN ("(\ (c.x), \ (C.Y))") println ("(\ (a.x), \ (A.Y))") assert (A = = C, "a = = B Failed ") A + = B assert (A! = C," A! = B failed ") C = a++ println (" (\ (a.x), \ (A.Y)) ") println (" (\ (c.x), \ (C.Y)) ") }vector2d_test ()


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.