16
votes
Dot product, ported from C to Swift
You can remove the size parameter as arrays know their own size in Swift, and use the zip, ...
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 ...
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 ...
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,...
11
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 ...
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 ...
10
votes
Accepted
Reading bytes from packet
One things for certain... an array of tuples which is basically being used as a glorified dictionary is never going to suffice as an acceptable data model. Moreover, an array of bytes isn't really ...
9
votes
Accepted
Swift HackerRank Balanced Brackets
readStrings() is better named readCharacters() because that is what it does.
I prefer ...
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 ...
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 { }:
...
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 ...
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 ...
8
votes
"What To Do" App
The instance variables
var userText : String = " "
var randomNumber : Int = 0
are not needed: userText is only used within <...
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 ...
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.) ...
7
votes
Accepted
Star Rating in Swift
You could create a helper routine getStarImage:forRating: and use that to set up each star:
...
7
votes
Accepted
Type to byte array conversion in Swift
Using a dedicated enumeration type ByteOrder is a good idea.
CFByteOrder is (ultimately) a type alias for ...
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 ...
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:
...
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*\...
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 ...
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:
...
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 ...
6
votes
Accepted
Updating number of votes with a "Like" button
Formatting
On the whole, there are several indentation problems through out your code. Here is made the most obvious example:
...
6
votes
Accepted
Implementing the Hashable Protocol in Swift with the DJB hash function
The Hashable protocol has only one single requirement:
Axiom: x == y implies ...
6
votes
Swift replacement for C's for-loop
Python also has no C-style for-loop. A lot of Python novices write clumsy while-loops as a result. However, once you learn the Pythonic idioms for looping, you'll wonder why other languages aren't ...
6
votes
Music player in Swift
Let's talk very generally about AVAudioPlayer.
There are a few relevant methods to discuss here:
play
...
6
votes
Accepted
Music player in Swift
class ViewController: UIViewController {
I noticed that your class is called ViewController. There are two things to comment ...
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 ...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
swift × 669ios × 219
performance × 51
programming-challenge × 45
swift3 × 43
beginner × 38
strings × 30
array × 27
algorithm × 26
uikit × 23
object-oriented × 22
json × 21
cocoa × 20
generics × 19
design-patterns × 17
functional-programming × 14
comparative-review × 14
protocols × 14
swiftui × 14
time-limit-exceeded × 13
primes × 12
image × 12
mvc × 12
asynchronous × 12
extension-methods × 12