Upload
hsieh-ching-fan
View
297
Download
1
Embed Size (px)
Citation preview
Swift Study Group Basic Operator, String
and Characters2015/8/11
Joe
Outline
• Basic Operators
• Strings and Characters
Basic Operators
Kind of Operators
• Unary: -a, !b, i++
• Binary: 2+3
• Ternary: a ? b : c
Assignment Operator
• let a = 1
• let (a,b) = (1,2)
• if x = y { } : the behavior of assigning value will not return value
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
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++
Increment and Decrement Operators
var i = 1 i++ i i = 1 ++i i
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(||)
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 ?
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
Range Operators
• Closed Range Operator: 1…5
• Half-Open Range Operator: 1..<5
Strings and Characters
Composition of String
• String -> Character -> extended grapheme cluster -> Unicode scalar
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
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
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)”
Unicode
• Unicode is an international standard for encoding, representing, and processing text in different writing systems.
Unicode Scalars
• A Unicode scalar is a unique 21-bit number for character or modifier, such as U+0061 for “a” U+1F425 for
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\""
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}"
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.
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
Counting Characters
var word = "cafe" count(word)
word += "\u{301}" count(word)
Accessing and Modifying a String
• Swift strings cannot be indexed by integer values.
• Using String index instead.Type: String.Index
String.Index• startIndex
• endIndex
• startIndex.successor()
• endIndex.predecessor()
• indices
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.
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.
Prefix and Suffix Equality
• hasPrefix(_:)
• hasSuffix(_:)
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
Reference
• The Swift Programming Language
• https://gist.github.com/joehsieh/5dffe9a5e49421c8cde5