xwu
(Xiaodi Wu)
May 2, 2020, 6:43pm
3
There have been extensive discussions on this topic which you may be interested to read.
If, after reviewing these links, you feel like there's a new direction or perspective to be had on the topic, then by all means please do share! In that case, it can be helpful for the community if you'd write a short synopsis of what you learn from these readings so that we're not starting back at square one.
In Swift, we often need to make our code repeat. Swift has made this easy to do while iterating through a range with a for loop. But you don't always need to iterate through a range when repeating code, take the following example.
struct Card {
var rank: Rank
var suit: Suit
enum Rank {
case ace
case king
case queen
case jack
case number(Int)
}
enum Suit: Int {
case hearts = 1, diamonds, clubs, spades
}
static let ne…
Dear Swift-Community,
I’d like to propose an addition of a useful method, especially for beginners that also makes Swift much more readable in some situations: The addition of a .times method to Integer type(s).
For example recently in one of my projects I wanted to test the scalability of an important piece of code and wrote this method:
func testPerfQualityInPercentWithoutQualityImprovements() {
self.measureBlock {
let expectedQuality = 33.33
0.stride(…
I'd like to propose a small enhancement to the repeat loop.
Currently if we want to perform 'count' iterations in a loop, we need to do
something like:
for _ in 0 ..< count {
//Do something here
}
It looks and feels a little awkward. We need to create an unnamed variable,
and it's easy to forget (especially for language newcomers) that the loop
starts with 0 and doesn't include count.
We can also do:
var i = 0
repeat {
//Some code
i += 1
} while i < 10
This is worse, in…
1 Like