Skip to main content
14 votes
Accepted

Extending CGPoint to conform to Hashable

I can see no advantage of computing the hash value from Int(self.x) and Int(self.y). As you already noticed, truncating the ...
Martin R's user avatar
  • 23.3k
13 votes
Accepted

Mocking UserDefaults in Swift

So first, as CAD97 rightly points out, if you simply define your protocol as identically matching the methods from UserDefaults you intend to use, then making ...
nhgrif's user avatar
  • 25.3k
12 votes
Accepted

On Knuth's "Algorithm L" to generate permutations in lexicographic order

Optimising the functional approach Iterative vs functional approach: Is it possible to use the (existing) functional methods without losing performance? I don't think it's possible to use the ...
Hamish's user avatar
  • 433
12 votes
Accepted

Fibonacci in linear time by using an extra pointer

Your function uses global variables, which is bad for several reasons: The variables must be reset before the function can be called again. The variables can be modified from outside of your function,...
Martin R's user avatar
  • 23.3k
11 votes
Accepted

Prime Number Generator in Swift

That is not the Sieve of Eratosthenes The Sieve of Eratosthenes computes multiples of each found prime to mark subsequent composite numbers in the sieve. Your algorithm computes the remainder of all ...
Martin R's user avatar
  • 23.3k
9 votes
Accepted

Swift HackerRank Balanced Brackets

readStrings() is better named readCharacters() because that is what it does. I prefer ...
Martin R's user avatar
  • 23.3k
9 votes
Accepted

Sequence-based enumeration of permutations in lexicographic order

Performance I'm not particularly surprised that the version of permutations(of:) which uses AnySequence and ...
Hamish's user avatar
  • 433
8 votes
Accepted

Chop off trailing newline for Swift's NSAttributedString

Your method works correctly, as far as I can see. For a read-only computed property, you don't need to enclose the getter method in get { }: ...
Martin R's user avatar
  • 23.3k
8 votes
Accepted

Sqlite.swift -- Selecting all rows and returning them in an array of tuples

Don't use tuples like this. The purpose of tuples is to make simple, short lived collections of values. For a use case like this, it is preferable to use a struct ...
Alexander's user avatar
  • 494
8 votes
Accepted

Project Euler problem 14 (longest Collatz sequence) in Swift 3

The main performance bottleneck of your code was already pointed out in the other answers: building lots of arrays (without need). I'll like to point out how some Swift techniques can be used to ...
Martin R's user avatar
  • 23.3k
8 votes

"What To Do" App

The instance variables var userText : String = " " var randomNumber : Int = 0 are not needed: userText is only used within <...
Martin R's user avatar
  • 23.3k
8 votes
Accepted

Showing a welcome screen once per day

There is some code duplication for the cases “greeting date has never been set” and “last greeting was yesterday or earlier.” This can be avoided if you use the nil-coalescing operator ...
Martin R's user avatar
  • 23.3k
8 votes
Accepted

Changing the color of some pixels in an image in Swift

There are two issues. The process of converting to and from UIColor objects is very expensive. The process of calling contains ...
Rob's user avatar
  • 2,552
8 votes
Accepted

Debounce and throttle tasks using Swift Concurrency

The problem is the use of a nonisolated function to initiate an asynchronous update of an actor-isolated property. (I'm surprised the compiler even permits that.) ...
Rob's user avatar
  • 2,552
7 votes
Accepted

Hex String to Bytes (NSData)

First note that your code does not detect invalid input data. For example, the string "XX" is just converted to a zero byte. Detecting invalid input with ...
Martin R's user avatar
  • 23.3k
7 votes
Accepted

Project Euler solution #2 using Swift 3

This post from Martin R is a good step in the right direction toward generalizing your problem. The helper function at the top of the post is already what you need for making a more generalized loop: ...
nhgrif's user avatar
  • 25.3k
7 votes

DFT (Discrete Fourier Transform) Algorithm in Swift

I think your code is doing DFT (Discrete Fourier Transform) and not FFT. You are doing \$O(n^{2})\$ operations in the 2 for loops. The FFT is supposed to be \$n*\...
lichi101's user avatar
7 votes

Prime Factorisation in Swift

To address your "main need" first: Your algorithm starts with a single-element array, and then repeatedly calls primeFact() to compute a new array, until the array ...
Martin R's user avatar
  • 23.3k
7 votes

Finding the number of perfect squares in an array

Mathematical correctness The used approach is incorrect for very large numbers, here is an example: ...
Martin R's user avatar
  • 23.3k
7 votes
Accepted

Minimum window substring - LeetCode challenge

Performance The main culprit for exceeding the time limit is here: while end < s.count { ... } A Swift string is a ...
Martin R's user avatar
  • 23.3k
6 votes
Accepted

Classification tree in Swift

So, let's focus on your Node class at the top. I started off, without looking at anything else, and just swiftlinting it. You have a 55 line file with 20 ...
nhgrif's user avatar
  • 25.3k
6 votes
Accepted

Safe init using failable guarded initializer

The separation into a non-failable and a failable init method is fine. The forced casts with as! are safe in your code, because it is verified before that the ...
Martin R's user avatar
  • 23.3k
6 votes

Swift FloatingPoint rounded to places

First of all one should be aware that most decimal fractions cannot be represented exactly by a binary floating point number. For example, ...
Martin R's user avatar
  • 23.3k
6 votes
Accepted

Cat fight (with rockets)

First, let me get out of the way that I'm not a "Functional Programmer", and I probably won't ever be. I'm also not an "Object-Oriented Programmer", or any other paradigm. I tend to use whatever the ...
CAD97's user avatar
  • 1,944
6 votes

Swift 3 Generic fetch request extension

Your code looks good, but there are some things which can be simplified. Additionally, the method can be improved to return a array of the actual managed object subclass type instead of ...
Martin R's user avatar
  • 23.3k
6 votes

UIButton subclass with animated 'shimmer' effect

Okay, so sometimes posting a question is the best way to figure out an answer yourself 🙃. I was looking for an alternative to having the gradient colors evenly spaced (my hack was to repeat the ...
MathewS's user avatar
  • 653
6 votes
Accepted

Password generator class

Type names in Swift use UpperCamelCase (compare API Design Guidelines), so it should be class CodeGen. I do not see any reason why the class must inherit from <...
Martin R's user avatar
  • 23.3k
6 votes

Wrapping the iOS triple buffering procedure in a generic swift class

Your platform-dependent code ...
Martin R's user avatar
  • 23.3k
6 votes
Accepted

Ring Timer Animation with Chasing Tail using CoreAnimation

There is one problem with your approach: Additional sublayers are added in draw() and in animateTimerReset() and never removed. ...
Martin R's user avatar
  • 23.3k
6 votes
Accepted

Grouping Date Objects by year and month

I'll focus first on static func sortDateByMonth(dateArray:[Date]) -> [[Date]] because that contains the main logic, then mention some other points, and ...
Martin R's user avatar
  • 23.3k

Only top scored, non community-wiki answers of a minimum length are eligible