31
Swift Study Group Basic Operator, String and Characters 2015/8/11 Joe

Basic Operator, String and Characters in Swift

Embed Size (px)

Citation preview

Page 1: Basic Operator, String and Characters in Swift

Swift Study Group Basic Operator, String

and Characters2015/8/11

Joe

Page 2: Basic Operator, String and Characters in Swift

Outline

• Basic Operators

• Strings and Characters

Page 3: Basic Operator, String and Characters in Swift

Basic Operators

Page 4: Basic Operator, String and Characters in Swift

Kind of Operators

• Unary: -a, !b, i++

• Binary: 2+3

• Ternary: a ? b : c

Page 5: Basic Operator, String and Characters in Swift

Assignment Operator

• let a = 1

• let (a,b) = (1,2)

• if x = y { } : the behavior of assigning value will not return value

Page 6: Basic Operator, String and Characters in Swift

Arithmetic Operators• +: It is also supported for String concatenation.

• %: It is also operate on floating-point numbers

• Question:

• -8 % 3

• 5 / -2

• 8 % 2.5

Page 7: Basic Operator, String and Characters in Swift

Increment and Decrement Operators

• If the operator is written before the variable, it increments the variable before returning its value.

• ++i

• If the operator is written after the variable, it increments the variable after returning its value.

• i++

Page 8: Basic Operator, String and Characters in Swift

Increment and Decrement Operators

var i = 1 i++ i i = 1 ++i i

Page 9: Basic Operator, String and Characters in Swift

Others• Unary Minus Operator: -3

• Unary Plus Operator: +3

• Compound Assignment Operators: a += 2

• Ternary conditional operator: hasHeader ? 2 : 3

• Logical Operator: NOT(!), AND(&&), OR(||)

Page 10: Basic Operator, String and Characters in Swift

Comparison Operators• a === b

• a != b

• a > b

• a < b

• a >= b

• a <= b

• a === b

let a = NSObject() let b = NSObject() let c = a a === b a === c

How about string comparison ?

Page 11: Basic Operator, String and Characters in Swift

Nil Coalescing Operator

• The nil coalescing operator (a ?? b) unwraps an optional if it contains a value, or returns a default value b if a is nil.

• (a != nil) ? a! : b

Page 12: Basic Operator, String and Characters in Swift

Range Operators

• Closed Range Operator: 1…5

• Half-Open Range Operator: 1..<5

Page 13: Basic Operator, String and Characters in Swift

Strings and Characters

Page 14: Basic Operator, String and Characters in Swift

Composition of String

• String -> Character -> extended grapheme cluster -> Unicode scalar

Page 15: Basic Operator, String and Characters in Swift

String• Literal: “ ”

• Empty String: a = “”, a = String(), a.isEmpty

• Mutability: var s = “Hello” s+=“World”

• Strings are value types not reference types.

• Concatenating with Character: let exclamationMark: Character = “!” s += exclamationMark

Page 16: Basic Operator, String and Characters in Swift

Characters

• String type represents a collection of Character value in a specified order.

• String value can be constructed by passing an array of Character values as an argument to its initializer

Page 17: Basic Operator, String and Characters in Swift

String Interpolation

• String interpolation is a way to construct a new String value from a mix of constants, variables, literals and expressions by including their values inside a string literal.

• let multiplier = 3let message = “\(multiplier) times 2.5 is \(Double(multiplier) * 2.5)”

Page 18: Basic Operator, String and Characters in Swift

Unicode

• Unicode is an international standard for encoding, representing, and processing text in different writing systems.

Page 19: Basic Operator, String and Characters in Swift

Unicode Scalars

• A Unicode scalar is a unique 21-bit number for character or modifier, such as U+0061 for “a” U+1F425 for

Page 20: Basic Operator, String and Characters in Swift

Special Character in String Literal (1/2)

• Special Characters

• \0 null character

• \\ backslash

• \t horizontal tab

• \n line feed

• \r carriage return

• \” double quote

• \’ single quote

let message = "\"Hello World\""

Page 21: Basic Operator, String and Characters in Swift

Special Character in String Literal (2/2)

• An arbitrary Unicode scalar, written as \u{n}, where n is 1-8 digit hexadecimal number with a value equal to a valid Unicode code point.

var str = "\u{1F425}"

Page 22: Basic Operator, String and Characters in Swift

Extended Grapheme Clusters(1/2)

• Every instance of Swift’s Character type represents a single extended grapheme cluster.

• An extended grapheme cluster is a sequence of one or more Unicode scalars that produce a single human-readable character.

Page 23: Basic Operator, String and Characters in Swift

Extended Grapheme Clusters(2/2)

is composed of one or two Unicode Scalars which is a single extended grapheme cluster

is composed of one or three Unicode Scalars which is a single extended grapheme cluster

Page 24: Basic Operator, String and Characters in Swift

Counting Characters

var word = "cafe" count(word)

word += "\u{301}" count(word)

Page 25: Basic Operator, String and Characters in Swift

Accessing and Modifying a String

• Swift strings cannot be indexed by integer values.

• Using String index instead.Type: String.Index

Page 26: Basic Operator, String and Characters in Swift

String.Index• startIndex

• endIndex

• startIndex.successor()

• endIndex.predecessor()

• indices

Page 27: Basic Operator, String and Characters in Swift

Inserting and Removing• insert(_:atIndex) : To insert a character into a

string at a specified index.

• splice(_:atIndex) : To insert another string at a specified index.

• removeAtIndex(_:atIndex): To remove a character from a string at a specified index.

• removeRange(_:range): To remove a substring at a specified range.

Page 28: Basic Operator, String and Characters in Swift

String and Character Equality

• ==, !=

• Two string values are considered equal if their extended grapheme clusters are canonically equivalent if they have the same linguistic meaning and appearance.

Page 29: Basic Operator, String and Characters in Swift

Prefix and Suffix Equality

• hasPrefix(_:)

• hasSuffix(_:)

Page 30: Basic Operator, String and Characters in Swift

Unicode Representation of Strings

• When a Unicode string is written to a text file or some other storage, the Unicode scalars in that string are encoded in one of several Unicode-defined encoding forms.

• UTF-8

• UTF-16

• Unicode Scalar

Page 31: Basic Operator, String and Characters in Swift

Reference

• The Swift Programming Language

• https://gist.github.com/joehsieh/5dffe9a5e49421c8cde5