myArray.remove(at: 0 -or any number-)
myArray.removeLast()
Back
passing an external argument label into a function
Front
an external argument label is just what appears in the parameter of a function
func iLikeSwift(thisMuch amountLiked: Int)
--
iLikeSwift(thisMuch: 2)
Back
to make sure the code does not crash with nil
Front
use an IF LET statement
Back
important .notation
Front
.value - gives the value of a button (like a UIStepper)
sender.tag - gives the value of a sender (like a UILabel)
Back
reversing array
Front
print(" STRIDE")
for index in stride(from: studentNames.count-1, through: 0, by: -1) {
print(studentNames[index])
}
print(" REVERSED w/range")
for index in (0...studentNames.count-1).reversed() {
print(studentNames[index])
}
print(" REVERSED ITERATING")
for student in studentNames.reversed() {
print(student)
}
question == 1 ? answer_if_true: answer_if_false
ex: let resultString = denominator != 0 "\(numerator/denominator) : "Can't divide by Zero"
Back
rounded function
Front
called with .rounded()
/// Rounds this value to an integral value using "schoolbook rounding."
*use for anything with decimals (doubles)
Back
to sum quizzes up (72, 81, 89, 95, 92)
Front
var quizzes = [72, 81, 89, 95, 92]
var sum = 0
for index in 0..<quizzes.count{
sum = sum + quizzes[index]
}
-this is going to take the first element, add it to zero, take the next element, add it to 72, etc...
Back
add an element into an array
Front
myArray.append(String, Int, Bool, etc)
Back
power function
Front
pow(Double, Double)
1st double = number
2nd double = to the power of the 2nd double
Back
if let statement
Front
if let input = Double(temperatureField.text!) {
-execute this code-
} else {
-do this code-
}
what happens when the "input" is NOT a double, the code will skip to the else statement
Back
to create a new array in random order
Front
myArray.shuffled()
Back
make sure that the same statement is not repeated
Front
var index = -1 -put -1 because it's out of range, so won't be skipped-
let messages = [...,..,.....,..]
var newIndex: Int
repeat {
newIndex = Int.random(in: 0..<messages.count)
} while index == newIndex
index = newIndex
messageLabel.text = messages[index]
or
var randomPhrase = ""
repeat {
randomPhrase = answerArray.randomElement()!
} while resultLabel.text == randomPhrase
resultLabel.text = randomPhrase
Back
to create a random number
Front
var randomNumber = Int.random(in: 0..<myArray.count-or any number-)
Back
to see if an array has nothing
Front
myArray.isEmpty
Back
or statement
Front
||
Back
find out how many rolls it takes to get a six
Front
var diceRoll: Int
var numberOfRolls = 0
repeat {
diceRoll = Int.random(in: 1...6)
numberOfRolls += 1
} while number != 6
Back
to make sure the array does not repeat
Front
var newAddresses = [String]()
for address in addresses {
if newAddress.contains(address) == false {
newAddress.append(address)
}
}
addresses = newAddresses
Back
the print-object command
Front
In the LLDB (Line Level Debugger)
po -swift expression-
used to display the value of the swift expression
Back
add an array to a new array with .prefix and .suffix
Front
var player1Cards = Array(shuffledCards.prefix(halfwayPoint))
-taking cards from the "prefix" of 26, so everything from 0 to 25-
var player2Cards = Array(shuffledCards.suffix(halfwayPoint))
-taking cards from the "suffix" of 26, so everything from 26 on-
Back
using mutable parameters to swap Bernie and Pete
Front
var firstPlace = "Bernie"
var secondPlace = "Pete"
func swapNames (first: inout String, second: inout String) {
let temp = first
first = second
second = temp
}
Back
to get random part of an array
Front
var randomNumber = Int.random(at: 0..<myArray.count)
print(myArray[randomNumber])
or
myArray.randomElement() -is an optional-
Back
Add an array to a new array
Front
var player1Cards = Array(deck[0..<26])
var player2Cards = Array(deck[26.<52])
player1Cards now has an array with a count of 26 "cards" with values that are from the the deck
player2Cards now has an array with a count of 26 "cards" with values that are from the the second half of the deck