Swift Basics-2

Source: Internet
Author: User

First, the basic operator

let a = 5

var b = ten

b = A

if a = b{

The assignment operator in Swift does not return itself as a value, so compiling is illegal, helping developers avoid mistakes, very human language

}

Second, mathematical operators

let dog:Character = "?? "

let cow:Character = "?? "

let dogcow = dog + Cow

println(dogcow)

//Swift arithmetic operator does not allow overflow, by overflow operator to select the value overflow condition (a& + b)

The // remainder operator can be used for floating-point numbers flost

8 % 2.5 //equals 0.5

//unary minus operator

let three = 3

let minustree =-three

let plusthree =-minustree

println(minustree)

println(plusthree)

//unary plus operator

Three, scope operators

= = =! = = Two identity operator detects whether two object references are from the same object instance

Two ranges

Enclosing (a...b) means: A to B value

For index in 1...5{

println ()

}

Semi-closed

(a). b), Baotou does not pack tail

let names = ["Anna","Alex","BBSC","SDFs"]

Let count = names. Count

for i in 0... count-1{

println("Person \ (i) is called \ (Names[i])")

}

Four, characters and strings

Shortcut keys:

Option +y = "Y"

Emoji = control + commod +space

The string in Swift is not the actual value of the pointer

Initializing an empty string

var emptystring = ""

var anotheremptystr = String ()

If emptystring.isempty{

}

var variablestring = "Horse"

Variablestring + = "and carriage"

Countelements calculating the number of characters in a string

Let countstr = "Hahahh"

println (countstr count is \ (countelements (COUNTSTR)))

The length of the NSString is based on the number of UTF-16 encodings and not on Unicode

The string in swift does not necessarily occupy the same memory space.

string comparison

Let SOMESTR1 = "abc"

Let SOMESTR2 = "abc"

if someStr1 = = somestr2{

}

The string in Swift is not the actual value of the pointer

Prefix equal

Let animals = ["Meat: Tiger", "Carnivorous: Lion", "Grass: Sheep", "Grass: Cattle", "Grass: Horse")

var acount = 0

For animal in animals{

If animal.hasprefic ("meat") {

++acount

}

}

println ("Here are \ (acount) head carnivores")

Converting string case

Let normal = "could to help me"

Let Shoty = normal.uppercasestring//Uppercase

Let whispered = normal.lowercasestring//lowercase

The string encoding output is printed using the C language

. UTF8. utf16

Unicode scalar

. unicodescalars

Put a space on the output

For scalar in Dogstring.unicodescalars

{

Print ("\ (scalar)")

}

Swift Basics-2

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.