Swift Programming Language

Swift Programming Language

memorize.aimemorize.ai (lvl 286)
Section 1

Preview this deck

create a nested function

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

4 years ago

Date created

Mar 1, 2020

Cards (43)

Section 1

(43 cards)

create a nested function

Front

func f1 () -> Int { var y = 10 func f2() { y += 5 } f2() return y }

Back

Functions are a first-class type. This means that functions can be passed as arguments and/or return values

Front

TRUE

Back

create a new variable that will be assigned a string array later

Front

var a = [ ]

Back

create a new string array

Front

let a = String[]()

Back

create for-in loop to iterate 3 times

Front

for i in 0..3 { ... }

Back

switch's case statements do not need break

Front

TRUE

Back

create a function that returns another function as its value

Front

func f1() -> (Int -> Int) { func f2(n: Int) -> Int { return 1 + n } return f2 }

Back

You can refer to parameters by number instead of by name—this approach is especially useful in very short closures.

Front

TRUE

Back

declare an optional

Front

var optionalString: String? = "Hello"

Back

the constructor method for a class is named 'init'

Front

class c { var name: String init(name: String) { self.name = name }

Back

"create for-in loop to iterate over items in a dictionary by providing a pair of names to use for each key-value pair."

Front

let d = [ "x":[1,2,3], "y":[4,5,6], "z":[7,8,9] ] for (k,n) in x { for m in n {...} }

Back

create a function that returns a tuple

Front

func f() -> (Int, Int, Int) { return (1,2,3)}

Back

create a function that takes another function as one of its arguments

Front

func matches(condition: Int -> Bool) -> Bool { if condition(item) { return true } return false }

Back

add a new key and value to a dictionary

Front

d1["newkey"] = "newvalue"

Back

name four reserved words that cause loops

Front

for-in, for, while, and do-while

Back

example of switch statement

Front

let vegetable = "red pepper" switch vegetable { case "celery": let a = 1 case "cucumber", "watercress": let b = 2 case let x where x.hasSuffix("pepper"): let c = 3 default: let d = 4 }

Back

A closure passed as the last argument to a function can appear immediately after the parentheses.

Front

sort([1, 5, 3, 12, 2]) { $0 > $1 }

Back

use if let on an optional

Front

var optionalName: String? = "John" if let name = optionalName { greeting = "Hello, \(name)" }

Back

You have several options for writing closures more concisely. When a closure's type is already known, such as the callback for a delegate, you can omit the type of its parameters, its return type, or both.

Front

TRUE

Back

statements do not need a semicolon at the end

Front

TRUE

Back

code at the global scope is the main() function

Front

TRUE

Back

name two reserved words that are conditionals

Front

if and switch

Back

declare a variable

Front

var v

Back

Functions are actually a special case of closures. You can write a closure without a name by surrounding code with braces { }. Use 'in' to separate the arguments and return type from the body.

Front

numbers.map({ (number: Int) -> Int in let result = 3 * number return result })

Back

Values are never implicitly converted to another type

Front

TRUE. example: let s = label + String(int1)

Back

declare a constant

Front

let c

Back

example of for-in loop

Front

let scores = [2,6,8] for score in scores { .... }

Back

create a constant that is explicitly typed

Front

let c : Double = 70

Back

check if optional is nil

Front

optionalString == nil

Back

create a new variable that will be assigned a dictionary value later

Front

var d = [ : ]

Back

use if and let together to work with a variable whose value might be missing

Front

if let x = obj1 { x.name = "abc" }

Back

create an instance of a class by putting parentheses after the class name

Front

var shape = Shape()

Back

"You use for-in to iterate over items in a dictionary by providing a pair of names to use for each key-value pair."

Front

let nums= [ "Prime": [2, 3, 5, 7, 11, 13], "Fibonacci": [1, 1, 2, 3, 5, 8], "Square": [1, 4, 9, 16, 25], ] var largest = 0 for (kind, numbers) in nums{ for number in numbers { if number > largest { largest = number } } }

Back

Use deinit to create a deinitializer if you need to perform some cleanup before the object is deallocated

Front

TRUE

Back

create a function that takes a variable number of arguments

Front

func f(numbers: Int...) { }

Back

write a string that has an integer variable in it

Front

"I have \(apples) apples."

Back

Create a single statement closure that implicitly returns the value of its only statement.

Front

numbers.map({ number in 3 * number })

Back

declare and initialize a dictionary

Front

var x = ["key1": "val1","key2": "val2"]

Back

create while loop to iterate 10 times

Front

var n = 0 while n < 10 { n = n + 1 }

Back

create a new dictionary

Front

let d = Dictionary<String, Float>()

Back

declare and initialize a string array

Front

var x= ["abc", "def", "ghi"]

Back

Switches support any kind of data and a wide variety of comparison operations—they aren't limited to integers and tests for equality.

Front

TRUE

Back

what is the name of a value that might be nil?

Front

optional value

Back