Swift basic operator

Source: Internet
Author: User

Swift basic operator

let a = 4

var b = ten

b = a


Let (x, y) = (1, 3) // now x = 1 y = 3

addition operations can also be used for strings

let dog:String = "Dogs"

let cow:String = "Cows"

let dogcow = dog + cow


seek to

Let c: Int = 9

Let D: Int = 2

Let e = C % D


The sign of a value can be toggled using a prefix-(that is, a unary minus):

Let three = 3

Let minusthree =-three //Minusthree equals -3

Let plusthree =-minusthree //Plusthree equals 3, or " negative minus 3"


conditional operator if

var name = "Hello"

If name = = "Hello"


Print("Hello World")




Print("wrong \(name)")


Ternary conditional Operation (ternary Conditional Operator)


The ternary conditional operation is special because it is an operator with three operands, and its prototype is a problem? Answer 1: Answer 2. It succinctly expresses the operation based on the question of whether or not to make two choices. If the problem is true, return The result of answer 1 ; returns The result of answer 2 if it is not true .


let contentheight =

let hasheight = true

let rowHeight = contentheight + (hasheight ? : )

Print(rowHeight) //RowHeight = =

The interval operator (Swift provides two operators that facilitate the expression of a range of values.) )

For index in 1... 5 {

Print("\(index) * 5 = \(index * 5)")


Let namearray = ["Jay","Jack","Tom","Lucy"]

Let count = namearray. Count

For i in 0... count


Print (" section \ (i + 1) personal call \ (Namearray[i])")

Print ("\ (Namearray[i])")// report field pointer


Logical Operations


The operand of a logical operation is a logical Boolean value. Swift supports three standard logic operations based on the C language.

Logical non (!a)

Logic with (a && b)

Logic or (a | | b)

Logical Non-

The logical non-operation (!a) Deserializes a Boolean value so that true becomes falseandfalse to true.


Let allowenpty = false

If ! Allowenpty {



logic with (a && b) expresses that the value of the entire expression is true when only the values of A and B are true .

Let Entereddoorcode = true

Let Passedretinascan = false

If entereddoorcode && passedretinascan


Print("welcome!" )






logical OR logical OR (a | | b) is a by two consecutive | The middle operator that is composed. It indicates that one of the two logical expressions is true, and the entire expression is true.

Let Hasdoorkey = false

Let Knowsoverridepassword = true

If hasdoorkey | | knowsoverridepassword


Print("welcome!" )






output "welcome!"

Swift basic 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.