Swift notes (ii)--operator

Source: Internet
Author: User
Tags arithmetic operators

Basic Operators
Swift supports most of the standard C-language operators, and has made some improvements to help developers make fewer low-level mistakes, such as: when you should use = =, write down a =, if x = = y {...} is written as if x = y {...} You're tragic. When using the +,-, *,/, the results are overflow Out of the. You're still a tragedy. We have to do a lot of checking to make sure we don't spill. However, in swift, we no longer have to worry about these problems. The compiler's going to help us. Swift also provides a $ two operator that is not available in the two C languages. And... Next, let's look at how it's used in swift
Terminology (specifically listed in the original) what is a unary operator, a two-tuple operator, a trinocular performer, I'm not listed here.


Assignment Performer (=) assignment operator (=)(thank you for Swift Technical Exchange first platform (355277) group Friends (1651150754) pointed out typos)
var a = 5let b = 10a = b//Do not explain
Emphasis appears. Tuple assignment: Let (x, y) = (1, 2)//x = 1, y = 2 is the same
But assuming that the = is written in the IF, it is only compiled in Swift,I'll never have to worry about the wrong to write = =If x = y {...}//The compiler will prompt you here:Type ' () ' does not conform to protocol ' Logicvalue 'Other wordsThe compiler does not feel that the assignment operator is a bool expression
Arithmetic Executor arithmetic operators (thanks to Swift Technical Exchange first platform (355277) group Friends of the Day (1651150754) pointed out typos)
1 + 25-32 * 310.0/2.5 These are the same as C, but the Swift compiler checks whether the results of the operation will overflow, for example: var myuint:uint = 1myUInt = MyUInt-2//The compiler will prompt you:arithmetic operation ' 1-2 ' (on type ' UInt ') results in an overflow
But there are some special times. What do we do when we need it to overflow? Swift provides an operator that can overflow: &-This is not the first detail. In the final chapter, let's talk about overflow rules.
operator + also supports string connections at the same time. such as: "Hello," + "Kitty"//equal to "Hello, kitty"
Find the remainder operator (%) 9 4 results are 1//In some languages, the remainder is calculated using modIt is important to note that the hypothesis is a negative operation, for example9%-4 The result is still 1, to get the right results, it has to be written as:-9% 4 not availableOf Course, 9%-4 and-9 4 results are the same. The symbol after the executor is ignored
In addition, in swift, floating-point numbers can also use% to calculate the remainder, which is not possible in C and objective-c 8 2.5//Get Results 0.5
self-increment and decrement operatorsAs in C language, well, there will be a large number of brain remnants of the interviewer to use a++ and ++a to toss people anyway in the project, if you dare to take a++ and ++a to joke about writing easy to confuse the code of the person. 2 words do not say, direct dismissal is right (even the code is for people to read.) It is not clear that the machine should be read. What code do you write?)
Unary OperatorsThere's nothing to say ... Kindergarten children know to add a minus sign in front of 3, he is-3 ...
Compound OperatorsSame as C inside. var a = 1a + = 2the value of//a is 3.
comparison operator (return bool type)Inference equals = = inference is not equal to = inference greater than > inferred less than < inference is greater than or equal to >= inference is less than or equal to <= these operators can be used not only for numeric values but also for string inference in addition. Swift also provides two new comparison operators = = = and!=== to infer whether two references point to the same object, and this section of the class and struct will have a sample
Three mesh operator (?:)As question as the C language?

Answer1:answer2 equivalent to if question {answer1} else {Answer2}
Range Performer (I think it's inappropriate, and should be called self-expanding). And... The difference in the mathematical method of descriptive narrative is: a. b is equivalent to [a, b] //does not contain Ba...b equivalent to [A, b] //contains B I think it's a pit again .... Not easy to put = and = = problem to kill, and a. And... Is there a difference between the two problems?!
logical Operators with (&&) or (| |)   Non-(!) //nerves are not problematic!!!! in a language. There are two meanings of the same symbol in different positions!!! What are the meanings of the words!!! Look, this is the time for the 2B interviewer to play the kinky phase.
Sooner or later someone will write a code like this: let Allowedentry:bool?

= Falseif!allowedentry!  {...} //There is an exclamation mark before and after. What are the meanings of the words!!!! Spit it out again.
As for not giving a && B | | C | | D && e brackets denote the priority of the person. Again highly recommended dismissal


Swift notes (ii)--operator

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.