Solutions Pool

FeatureLink

Score count

Remove repeating questions

...

Countdown

var countdown = 10

while countdown > 0 {
    print("\(countdown)…")
    countdown -= 1
}

print("Blast off!")

/*
print:

10…
9…
8…
7…
6…
5…
4…
3…
2…
1…
Blast off!

*/

Source: https://www.hackingwithswift.com/quick-start/beginners/how-to-use-a-while-loop-to-repeat-work

Disable the field if it is empty

.disabled(newAttendeeName.isEmpty)

Image in a Text

Text("Hello, \(Image(systemName: "globe"))!")

Local Currency

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
   
*/

Source: https://www.hackingwithswift.com/quick-start/beginners/how-to-reuse-code-with-functions

Random Number in a defined range

Int.random(in: 1...1000)

// print: 728 (random number)

Source: https://www.hackingwithswift.com/quick-start/beginners/how-to-use-a-while-loop-to-repeat-work

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!

*/

Source: https://www.hackingwithswift.com/quick-start/beginners/how-to-use-a-while-loop-to-repeat-work

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.

Source: https://www.hackingwithswift.com/review/sixty/exiting-loops

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]

Source: https://www.hackingwithswift.com/quick-start/beginners/how-to-skip-loop-items-with-break-and-continue

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.

Source: https://www.hackingwithswift.com/quick-start/understanding-swift/why-would-you-want-to-exit-a-loop

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.

Watermark

Last updated