# Solutions Pool

| Feature                    | Link                                                                           |
| -------------------------- | ------------------------------------------------------------------------------ |
| Score count                | [HWS / Day-22](https://www.hackingwithswift.com/plus/solutions/guess-the-flag) |
| Remove repeating questions | [HWS / Day-22](https://www.hackingwithswift.com/plus/solutions/guess-the-flag) |

...

### Countdown

```swift
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

```swift
.disabled(newAttendeeName.isEmpty)
```

### Image in a Text

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

### Local Currency

{% code overflow="wrap" %}

```swift
let localCurrency: FloatingPointFormatStyle<Double>.Currency = .currency(code: Locale.current.currency?.identifier ?? "USD")
```

{% endcode %}

### Multiline TestFields

![](https://5317963-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F598GXhEFvy2PX7VpZjCw%2Fuploads%2FopRbSpDTUeJ9DzbZSyuD%2Fimage.png?alt=media\&token=452b8797-c309-41a3-a290-0c40e82116b7)

* <https://useyourloaf.com/blog/swiftui-multi-line-text-fields/>

### Parameters over Arguments

Always prioritize parameters (placeholders) over arguments (actual values).

```swift
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

```swift
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

```swift
// 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

```swift
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;

```swift
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;

```swift
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

```swift
ForEach(data.attendees) { attendee in
                    Text(attendee.name)
                }
                .onDelete{ indices in
                    data.attendees.remove(atOffsets: indices)
                } // Swipe-left yapinca ilgili row siliniyor.
```

### Watermark

{% embed url="<https://youtu.be/B6UaRlli5rw>" %}
