Hot answers tagged

28 votes
Accepted

Stack implementation using Swift

Let's start with some general remarks: Swift does not require semicolons after statements (but they are allowed). From what I have seen since Swift was introduced last year, most people do not write ...
  • 22.7k
25 votes

Swift 1.2 Singleton Implementation

You could simplify the above significantly: ...
20 votes
Accepted

Swift iOS - Call back functions

Very regularly when dealing with asynchronous stuff (among other things) in iOS development, we have to make a decision between three tools. Closures (blocks, callbacks, etc) ...
  • 25.2k
19 votes

Ultimate FizzBuzz

Your code looks good to me, and I have only some minor remarks and suggestions. An Optional has an implicit initial value of nil, so ...
  • 22.7k
18 votes

Swift 1.2 Singleton Implementation

First and foremost, printing text to the console is absolutely pointless for an iOS application. It's okay to do it as an easy way to test whether our code is working as intended, but we don't want ...
  • 25.2k
16 votes
Accepted

Getting WiFi SSID on iOS in Swift

The first thing that stands out to me is that you've created an entirely new class that exists purely to have this class method. This doesn't make a whole lot of sense. At a minimum, this method ...
  • 25.2k
15 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, ...
  • 251
14 votes

Layered Neural Network in Swift

assert(false, "Don't use assert like this!") ...
  • 25.2k
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 ...
  • 22.7k
13 votes
Accepted

Parse query checks and increments

Lately, when writing Code Review posts, I like to point out the first thing I notice when I look at someone code. This is the first thing I notice when I look at your code. Without looking at ...
  • 25.2k
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 ...
  • 25.2k
12 votes
Accepted

Project Euler, Challenge #5 in Swift

== false while found == false We'd prefer to see either this: while !found or this: <...
  • 25.2k
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,...
  • 22.7k
11 votes

Beginner stopwatch app

The primary problem I see here is the way we're keeping track of the time passed. NSTimer is not guaranteed to tick with the exact amount of time you passed it. ...
  • 25.2k
11 votes

Background execution in iOS

func backgroundExecutable(f: (() -> Void) -> Void) -> (() -> Void) This is madness and it takes a number of mental gymnastics to figure out what is ...
  • 25.2k
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 ...
  • 423
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 ...
  • 22.7k
10 votes

Project Euler, Challenge #5 in Swift

You are using a brute force attack to solve this problem but you should better think about it and use some maths. Right now you are checking if the number is evenly dividable by ...
  • 50.1k
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 ...
  • 25.2k
9 votes
Accepted

Checking two text field for match and checking for null

Your indentation is a bit wonky. Why is the first line indented? Why isn't the stuff in the inner-else indented correctly? There's no reason to println() our ...
  • 25.2k
9 votes
Accepted

Sprite-Kit/Swift game

You've posted quite a lot of code, and as such, for now I'm going to focus simply on a big picture overview of your code (and its organization). This is the first thing that stands out to me is that ...
  • 25.2k
9 votes

FizzBuzz in Swift 2

I would have started by looking over more than just the question in the linked question. You should have looked at the answers. First of all, while the added where...
  • 25.2k
9 votes
Accepted

Coredata delete all data in an entity

let appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate let context: NSManagedObjectContext = appDel.managedObjectContext! The type ...
  • 22.7k
9 votes
Accepted

Swift HackerRank Balanced Brackets

readStrings() is better named readCharacters() because that is what it does. I prefer ...
  • 22.7k
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 ...
  • 423
8 votes
Accepted

Equivalent of C-style "Static Local Variable" in Swift

The only work around for this that I've come up with thus far is something that looks like this: ...
  • 25.2k
8 votes
Accepted

Message factory

I can't recommend these string constants at all. We should be using an enum. Even if your enum is still string backed (which ...
  • 25.2k
8 votes
Accepted

C Socket API Call with Swift

Wrong memory management gethostbyname() returns a pointer to an internal structure. You don't have initialized that memory and don't own it, therefore you must not ...
  • 22.7k
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 { }: ...
  • 22.7k
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 ...
  • 494

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