• Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

Card Range To Study

through

image

Play button

image

Play button

image

Progress

1/20

Click to flip

Use LEFT and RIGHT arrow keys to navigate between flashcards;

Use UP and DOWN arrow keys to flip the card;

H to show hint;

A reads text to speech;

20 Cards in this Set

  • Front
  • Back

Collection Types

Swift provides three primary collection types, known as arrays, sets, and dictionaries, for storing collections of values. Arrays are ordered collections of values. Sets are unordered collections of unique values. Dictionaries are unordered collections of key-value associations.




Arrays, sets, and dictionaries in Swift are always clear about the types of values and keys that they can store. This means that you cannot insert a value of the wrong type into a collection by mistake. It also means you can be confident about the type of values you will retrieve from a collection.

To get the 4th item out of an array, which index number do we use?

3

let a = [1,2,3]


a.insert(4, atIndex: 3)




The code raises an error. Why?

The array is immutable and cannot be changed

Given an array:




var a = [10, 1, 12, 22, 96, 14]




Which line of code removes the 5th element in the array?

a.removeAtIndex(4)

let a = [1, 2, 3]


a[3]




The code above crashes. Why?

The index value provided is out of bounds

Given an array:




let a = [2.0, 1.1, 2.3, 5.4]




What is the array's type?

[Double]

True of False:




An array preserves the order of its elements, i.e., it is an ordered collection.

True

DICTIONARIES

l

Given the following dictionary:




let dict = [1: "someValue", 2: "anotherValue"]




What is the dictionary's type?

[Int : String]

Given the following code:




var dict = [1: "someValue", 2: "anotherValue"]




dict.updateValue("yetAnoterValue", forKey: 3)




What is the result?

We add a third key value pair to the dictionary

When we try to access a value using a key that doesn't exist in a dictionary, what result do we get?

A nil value

For-In Loops

l

For this challenge, we're going to use a for in loop to compute the multiplication table for 6 and append the results to an array.




To start off, I've created an empty array of type Int and assigned it to a variable named results. Note that we have to explicitly declare the type here so that the compiler knows this array will hold Int values.




Your task for this step is to create a for in loop that iterates over a range. Multiplication tables typically range from 1 to 10 (with 10 included) so the range you are iterating over should go from 1 to 10.




For in loops also define a constant that temporarily stores the value in the iteration process. For the loop you're writing, name this constant multiplier.

var results: [Int] = []



for multiplier in results {


print("\(1...10) time 6 is equal to \(1...10 * 6)") }

Inside the body of the loop, we're going to use the multiplier to get the multiple of 6. For example, if the multiplier is 1, then the multiple is 1 times 6, which is equal to 6.




Once you have a value, append it to the results array. This way once the for loop has iterated over the entire range, the array will contain the first 10 multiples of 6.

var results: [Int] = []




for multiplier in 1...10 {


results.append(multiplier * 6)


}






So here we start with the results array they give us. Now we're going to go through the range 1 to 10 as specified by the challenge. Every time through we're going to append the multiplier times 6. Our results array should end up looking like this: [6, 12, 18, 24, 30, 36, 42, 48, 54, 60]

WHILE LOOP

Sometimes we need to perform a set of actions while a condition is true. We don't have a predefined number of times, we just know that until the condition stops being true, we keep carrying out the action. In Swift this kind of logic is modeled with a set of while loops.

While Loop

/

Which of the following comparison operators is known as the “less than or equal to” operator?

<=

var isAvailable = true


var isCheap = true


var status: String


if !isCheap && !isAvailable {


status = "super rare"


} else if isCheap && isAvailable {


status = "not rare"


} else if !isCheap && isAvailable {


status = "somewhat rare"


} else {


status = "common"


}




What does status evaluate to?

"not rare"

Both the AND and OR operators use short circuit evaluation




True or false?

TRUE

What does the following statement evaluate to? (!true || !false) && (true && !false)

TRUE