let localCurrency: FloatingPointFormatStyle<Double>.Currency = .currency(code: Locale.current.currency?.identifier ?? "USD")
Multiline TestFields
Parameters over Arguments
Always prioritize parameters (placeholders) over arguments (actual values).
func printTimesTables(number: Int, end: Int) {
for i in 1...end {
print("\(i) x \(number) is \(i * number)")
}
}
printTimesTables(number: 5, end: 20)
/*
5: argument
number: parameter
20: argument
end: parameter
Whichever you choose,
you should place them in the order they were listed when you created the function.
E.g. you cannot write "printTimesTables(end: 20, number: 5)" swift will not allow it
*/
Random Number in a defined range until a particular target
// create an integer to store our roll
var roll = 0
// carry on looping until we reach 20
while roll != 20 {
// roll a new dice and print what it was
roll = Int.random(in: 1...20)
print("I rolled a \(roll)")
}
// if we're here it means the loop ended – we got a 20!
print("Critical hit!")
/*
print:
I rolled a 11
I rolled a 14
I rolled a 3
I rolled a 16
I rolled a 10
I rolled a 9
I rolled a 9
I rolled a 12
I rolled a 3
I rolled a 12
I rolled a 9
I rolled a 17
I rolled a 15
I rolled a 20
Critical hit!
*/
Remainder of multiple numbers
for i in 1...15 {
if i % 3 == 0 {
if i % 5 == 0 {
print("\(i) is a multiple of both 3 and 5.")
}
}
}
// print: 15 is a multiple of both 3 and 5.
Sum / Count until a certain point
Example 1;
let number1 = 10
let number2 = 14
var multiples = [Int]()
for i in 1...100_000 {
if i.isMultiple(of: number1) && i.isMultiple(of: number2) {
multiples.append(i)
if multiples.count == 10 {
break
}
}
}
print(multiples)
// print: [70, 140, 210, 280, 350, 420, 490, 560, 630, 700]
Example 2;
let scores = [1, 8, 4, 3, 0, 5, 2]
var count = 0
for score in scores {
if score == 0 {
break
}
count += 1
}
print("You had \(count) scores before you got 0.")
// print: You had 4 scores before you got 0.
Swipe-Left to Delete
ForEach(data.attendees) { attendee in
Text(attendee.name)
}
.onDelete{ indices in
data.attendees.remove(atOffsets: indices)
} // Swipe-left yapinca ilgili row siliniyor.