Section 1

Preview this deck

Empty Strings

Front

Star 0%
Star 0%
Star 0%
Star 0%
Star 0%

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Active users

0

All-time users

0

Favorites

0

Last updated

6 years ago

Date created

Mar 1, 2020

Cards (63)

Section 1

(50 cards)

Empty Strings

Front

initialize with "" or String() check with .isEmpty()

Back

break

Front

ends execution of control flow statement

Back

Iterating over a set

Front

for-in for iterating without order for item in setname.sort() {} for iterating with order

Back

print(_:separator:terminator:)

Front

separator -- string between printing arguments (def: " ") terminator -- string that ends printed output (def:
)

Back

Accessing and Modifying Sets

Front

.count (read-only) gives size .isEmpty tells if empty .insert(_: ) adds item .remove(_: ) returns item if removed or nil if not -- check with optional binding

Back

String Mutability

Front

Strings with "var" are mutable, "let" are not

Back

UInt8, UInt16, UInt32, UInt64

Front

Unsigned integer

Back

Error Handling

Front

define function that causes error func name() throws { } call function (or other code that might throw error) and catch do { try name() } catch Error.someError { //other code }

Back

Modifying arrays

Front

.append(element) to add to end [0] = ["String"] [1...4] = ["h", "e", "l"] changes indices 1-3 AND removes 4 insert(_: atIndex: ) inserts an obj at specific index removeAtIndex(_: ) removes object at index, returns that object removeLast() -- faster for last item removing, doesn't query .count

Back

Integer and Floating Point Conversion

Front

When using literals, it doesn't need to be explicit, but when using variables, need to use constructors: Double(num) for example

Back

typealias

Front

Creates new ALIAS (not a class extending) for a type: Ex. typealias AudioSample = UInt8

Back

naming variables/constants

Front

cannot contain whitespace, math symbols, arrows, private use code points, can't start with a number

Back

unary minus operator: -

Front

changes sign of number

Back

Characters

Front

access with .characters property for iteration add to string with .append(charactervar) create as any type get length of string with characters.count

Back

String Equality

Front

use == to check for canonical equivalence (basically, do they look the same in english)

Back

Half Open Range Operator : a...<b

Front

from a to b, not including b

Back

iterating through arrays

Front

for(var i = 0; i < arr.characters.count; i++) traditional for item in arr enhanced for (index, value) in arr.enumerate() enhanced w/index

Back

constant

Front

declare with "let" use if variable will never change

Back

assert

Front

var age = 3; Ex. assert(age >= 0, "A person's age can't be less than 0") STOPS execution of code if condition false, prints error When to use: whenever a condition has the potential to be false, but must definitely be true in order for your code to continue execution

Back

Accessing Tuple values

Front

1. Can use indices var intstring = (404, "error") intstring.0 -> gives you 404 2. Can decompose var (num, str) = intstring or if you just want one var (num, _) = intstring

Back

repeat-while

Front

repeat { } while condition

Back

getting key and value lists

Front

[Type](dictname.keys) [Type](dictname.values)

Back

continue

Front

stops exec current iteration and goes to next

Back

Hash values for Sets

Front

Objects in a set must be Hashable -- implements gettable Int property called hashValue and implements the == operator

Back

Range Operator: a...b

Front

from a to b inclusive

Back

Iterating through a dictionary

Front

for (key, value) in dictname { // do what your heart tells you } can iterate through dictname.keys or dictname.values as well

Back

Optional

Front

Either contains a value or contains nil; Ex Int(string) returns Int?

Back

Accessing and Modifying a Dictionary

Front

use .count for size and .isEmpty for empty checking Adding: dictname["key"] = "value" OR dictname.updateValue("value", forKey: "key") --> returns old value as optional so you can check if update happened Removing: dictname["key"] = nil OR dictname.removeValueForKey(_: ) --> returns removed or nil

Back

String indices

Front

each character has index (like character array) but not with integer values .startIndex returns first index (like) .predecessor() index before .successor() index after .startIndex.advancedBy(integernum) -> start + that integer .endIndex = .characters.count // so don't use this in a subscript argument

Back

switch cases

Front

no implicit fallthrough, need to match all possible cases (with or without a default), use where to check additional conditions can accept ranges as cases with Tuples: (_, 0) <- left is a wildcard case can have ranges in Tuples too! a case can bind a var like case let x: \\code or case (let x, 0): \\for a tuple or case let (x, y): \\ also for tuple

Back

fallthrough

Front

adds fallthrough to switch case, since switch does not have implicit fallthrough

Back

unary plus operator: +

Front

doesn't do anything

Back

Tuples

Front

Lets you combine different types into one compound type: Ex. var intstring = (404, "error") can name types like var intstring : (status: Int, message: String) = (404, "error"), can even typealias that with the named params

Back

String interpolation

Front

var statement = "statement" print("hello, this is a \(statement)")

Back

Array Instantiation

Front

Ex. var someInts = [Int]() or can use [] --> gives no type though or var someInts = [Int](count: 3, repeatedValue: 0) or var someInts = [1, 2, 3]

Back

Nil Coalescing Operator: ??

Front

a ?? b => a != nil ? a! : b explanation: if a is not nil, a is unwrapped and returned, else b is returned used for concision

Back

hasPrefix and hasSuffix

Front

check if string starts with or ends with a string

Back

remainder operator: %

Front

a % b => a = (b x multiplier) + remainder ex. -9 % 4 = -1 bc -9 = (4 x -2) + -1 b's sign doesn't change answer can also operate on floating point numbers

Back

nil

Front

Valueless state (not a pointer to a nonexistent object) var name:Type? sets name to nil before it is initialized

Back

Optional Binding

Front

if let name = someoptional { // use name, which is limited to the scope of this if statement } can use multiple and check Boolean condition with where

Back

Set

Front

A set stores distinct values of the same type in a collection with no defined ordering instantiated with Set<Element>() or (if type annotated) ["item, ... ] with array literal implies that after type defined setting it equal to [] makes an empty set

Back

type annotation

Front

var varname : Type means "of type Type" can give multiple variables same type var one, two, three : Int

Back

Float

Front

32-bit floating point

Back

variable types

Front

first initialization defines type, cannot change after

Back

Int

Front

Swift default integer type that takes word size of platform (Int32 for 32-bit, Int64 for 64-bit) Int8, Int32, Int16, Int64

Back

Double

Front

64-bit floating point

Back

Dictionary

Front

type name is Dictionary<Key, Value> but can initialize with var dict = [Int : String]() ^ example can make empty after type defined with [:] dictionary literal: [key 1: value 1, key 2: value 2, key 3: value 3] also works

Back

Check for empty arrays

Front

.isEmpty

Back

Set operations

Front

.intersect(_: ) .union(_: ) .subtract(_: ) .exclusiveOr(_: ) basically the opposite of intersect .isSubsetOf(_: ) .isSupersetOf(_: ) .isDisjointWith(_: ) .isStrictSubsetOf(_: ) or .isStrictSupersetOf(_: )

Back

Booleans

Front

true or false non-booleans can't be used as conditional statements var i = 1; ex. if i {} would not compile

Back

Section 2

(13 cards)

trailing closures

Front

Back

guard

Front

checks a boolean (like an optional binding) as an if statement would, but requires an else, and stops execution of current scope if boolean not true and exec else; can't execute code as a result of the if statement, can only check condition

Back

external and internal parameters

Front

external -- used when calling, written before internal in func name declaration internal -- used in method default: first param not named, rest use internal func name(person person: String, _ name: String) -> String {} ^ person (the 1st one) is external name, second param uses _ so it doesn't need a name when being called

Back

variable parameters

Front

func variableparam(var string:String) {} creates a new copy of string as a variable inside function, because parameters are constants by default

Back

#available(platformname version, ... , *)

Front

can be placed after if or guard to check for version ex. guard #available(iOS 9, *) else { return }

Back

implicit return for single expression closure

Front

if only one statement in method, return type inferred, so return statement can be omitted

Back

variadic parameters

Front

func variadicexample(numbers: Double...) {} numbers is a [Double]

Back

inferring types in closures

Front

Swift can infer the types of parameters and return in closure, so type declarations not needed

Back

function types

Front

func name() -> String {} has a type () -> String can make a variable to store functions of this type and use them (Ex. var stringfunction : () -> String = name) where name is the function created above also means that these can be used as parameters in functions and as RETURN types (can return functions)

Back

shorthand argument names

Front

can access params of a closure using $index (Ex. in sort(arr) { $0 > $1 } )

Back

closures

Front

self-contained blocks of functionality, can capture and store references to any constants and variables from the context in which they are defined; ex. Swift sort(_: _:) lets you define a sorting closure

Back

function return types

Front

func name() -> String? made String optional for case where nil is returned

Back

inout parameters

Front

inout string: String allows changes to parameter to persist after method executes

Back