Hot answers tagged

45 votes
Accepted

Is this FizzBuzz Swift-y?

Is this FizzBuzz Swift-y? Kinda, but it could be a lot better. Here's what I would do to fix it: Extrapolate this code into a method, then call the method from the ...
user avatar
  • 21.7k
32 votes
Accepted

Rewrite code from Objective-C to conform with Swift power tools and concise style

The problem here isn't that your Swift looks like Objective-C. The problem is that, because you're developing a toy app, you've used a toy design, and now you don't like the toy design. Probably you ...
user avatar
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 ...
user avatar
  • 22.3k
25 votes

Swift 1.2 Singleton Implementation

You could simplify the above significantly: ...
user avatar
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) ...
user avatar
  • 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 ...
user avatar
  • 22.3k
18 votes
Accepted

Int extension for translating integer to plain English

So Apple already has an implementation for this, using NSNumberFormatter. A bit easier to implement than what you have now ;). ...
user avatar
  • 21.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 ...
user avatar
  • 25.2k
17 votes
Accepted

Piano app in Swift

I think this should work. The key (pun intended) to making this work is to take advantage of the tag property of UIButton. In ...
user avatar
  • 406
16 votes
Accepted

Project Euler #2 (Even Fibonacci numbers) in Swift

The following is similar to Flambino's approach, but creates a Swift SequenceType so that you can use the Swift library functions ...
user avatar
  • 22.3k
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 ...
user avatar
  • 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, ...
user avatar
  • 251
14 votes

Layered Neural Network in Swift

assert(false, "Don't use assert like this!") ...
user avatar
  • 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 ...
user avatar
  • 22.3k
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 ...
user avatar
  • 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 ...
user avatar
  • 25.2k
12 votes

Is this FizzBuzz Swift-y?

In a for loop in Swift, the parentheses are optional, and from all of Apple's book and sample code, they are usually omitted--you did so yourself on the ...
user avatar
  • 415
12 votes
Accepted

Project Euler, Challenge #5 in Swift

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

Project Euler #2 (Even Fibonacci numbers) in Swift

I can only suggest a faster* math-based algorithm: The fibonacci numbers are: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 and etc. The even ones are: 0 at index 0, 2 at index 3, 8 at index 6, 34 at index 9 and ...
user avatar
  • 5,160
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. ...
user avatar
  • 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 ...
user avatar
  • 25.2k
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 ...
user avatar
  • 22.3k
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 ...
user avatar
  • 50k
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 ...
user avatar
  • 25.2k
10 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 ...
user avatar
  • 413
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 ...
user avatar
  • 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 ...
user avatar
  • 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...
user avatar
  • 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 ...
user avatar
  • 22.3k

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