Questions tagged [swift]

Swift is a multi-paradigm, compiled programming language developed by Apple for iOS and OS X development.

Filter by
Sorted by
Tagged with
23
votes
9answers
3k views

Is this FizzBuzz Swift-y?

...
9
votes
1answer
303 views

Sequence-based enumeration of permutations in lexicographic order

This is follow-up on On Knuth's "Algorithm L" to generate permutations in lexicographic order, where I presented the following method to enumerate all permutations of array elements in ...
15
votes
4answers
2k views

Int extension for translating integer to plain English

This code was inspired by this question: Number to Words ...
20
votes
4answers
3k views

Project Euler #2 (Even Fibonacci numbers) in Swift

I figured working through Project Euler problems in Swift would be a good way to learn any tips or tricks. For example, tuples are something I'm not used to using, but they proved useful here. Using ...
14
votes
3answers
2k views

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

I needed a method to generate all permutations of given elements, so I decided to implement "Algorithm L (Lexicographic permutation generation)" from Donald E. Knuth, "GENERATING ALL PERMUTATIONS" in ...
11
votes
2answers
2k views

Computing the integer square root of large numbers

The "integer square root" of a non-negative integer \$ n \$ is defined as the largest integer not greater than \$ \sqrt{n} \$: $$ \operatorname{isqrt}(n) = \lfloor \sqrt{n} \rfloor = \max \{ k \in \...
8
votes
1answer
1k views

Prime Number Generator in Swift

I would like to make (or improve my function) a function that returns an array of integers containing all the prime numbers from 2...n that can execute as quickly ...
7
votes
2answers
408 views

Postponed Prime Sieve in Swift

Motivated by this question, I looked out for other "infinite prime generators", i.e. functions which produce the list of prime numbers in increasing order and do not have an a-priori upper limit (such ...
6
votes
3answers
11k views

Save token id into a database in Swift

I want to save my users tokens id into my database using POST request in Swift (here PHP code here). I am a bit confused about synchronous and asynchronous request. Is this code the right way to do ...
2
votes
1answer
678 views

Unbounded Sieve of Eratosthenes in Swift

I've spent a while teaching myself Swift, and decided to take on the challenge of writing an unbounded Sieve of Eratosthenes to challenge myself. This is actually the first time I've written an ...
8
votes
2answers
2k views

Recursive flattening of Swift sequences

In Flatten to get all child controls of certain type in a UIView, methods were discussed to recursively flatten a tree-like structure in Swift, resulting in an array of all elements. Motivated by ...
6
votes
1answer
475 views

Project Euler #10 in Swift - Summation of primes

I just finished Project Euler #10 in Swift, and since there is not any version yet on Code Review, I would like to have some comments on what I did to try to improve it. I hope I learned some from ...
24
votes
1answer
11k views

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

I started a project with Objective-C and rewrote it with Swift. The project contains two UITableViewControllers: MasterViewController and DetailViewController. ...
15
votes
1answer
6k views

IBDesignable UICheckbox

A more up-to-date version of this control can be found on GitHub. One UI control that has always been mysteriously missing from Xcode's interface builder is some sort of checkbox. ...
8
votes
6answers
2k views

DFT (Discrete Fourier Transform) Algorithm in Swift

I am looking to replicate in Swift what the FFT function does in Matlab. Essentially, it takes an arbitrary length signal (not necessarily a multiple of \$2^n\$) and gives real and complex DFT ...
24
votes
1answer
688 views

Ultimate FizzBuzz

Swift's SequenceType is a useful means of generating a sequence of values, and it makes it particularly useful iterate over these values. I don't really have much ...
9
votes
0answers
895 views

Interview coding challenge for iOS Part 2 - the application in Objective-C and Swift

I recently interviewed with a company that needed a C/C++ programmer to work on the iOS side of the products. The job description indicated they needed someone with 4 years of Objective-C and iOS ...
8
votes
4answers
1k views

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

I was trying to solve Project Euler:Problem 14 using Swift 3, but it takes ages to give me an answer, which is a sign that my code is absolute garbage performance-wise. What could I do to increase the ...
7
votes
1answer
932 views

Hangman in Swift

I am a Java programmer trying to learn the ways of Swift. I coded a Hangman game in Xcode. I was wondering what I could improve, specifically whether I used delegation correctly and if there is ...
5
votes
2answers
653 views

Asynchronous HTTP JSON request

For asynchronous requests, I use Alamofire. I have created one method for requesting async data. ...
5
votes
1answer
303 views

Sierpinski Triangle

I am a Java programmer who recently started learning some Swift. I made an app that displays the Sierpinski triangle and adds triangles if the user pans up, and zooms in and out using the pinch ...
10
votes
1answer
579 views

Finger painting code

I have a very simple view that handles touch events and draws accordingly. It's nothing significant, but it does use a bit more CPU than I would like (35%). Again, it is the bare minimum (<90 lines)...
10
votes
2answers
648 views

Swift replacement for C's for-loop

I recently wrote an entry on my blog regarding unit testing using prime numbers as an example. When I wrote the entry, I wrote my code keeping in minding that proposal SE-0007 has been accepted for ...
6
votes
1answer
543 views

Project Euler #5 (Smallest multiple) in Swift

I found this one a little bit easier than the previous problems, but I was already familiar with the GCD/LCM formulas. ...
8
votes
2answers
2k views

Fraction (rational number) structure with custom operators

First, the struct itself: ...
7
votes
2answers
2k views

Generic NSMapTable replacement written in Swift

This is my attempt at writing a generic NSMapTable with weak keys and strong values (after your feedback I'll be trying to write Strong-Key/Weak-Value and Weak-Key/...
7
votes
3answers
203 views

Leetcode 38: The “count-and-say” sequence

38. Count and Say The count-and-say sequence is the sequence of integers with the first five terms as following: ...
7
votes
1answer
142 views

Swiftly counting rooms in a floor plan

Inspired by recent questions about counting the rooms in a floor plan (1, 2, 3), here is my attempt to solve the problem with a Swift program. The problem (from “Counting Rooms” on CSES) is: You ...
7
votes
2answers
1k views

Project Euler #3 (Largest prime factor) in Swift

As mentioned in Project Euler #2 in Swift, I intend to work my way through Project Euler using Swift to make sure there aren't any tricks I'm missing. This is the problem statement for #3: The ...
7
votes
1answer
327 views

Calculator implementation

With some background and experience in PHP & Python, I'm trying to learn Swift by myself (web, videos, Ray Wenderlich books). I've read that a good first project for beginners is to try and write ...
5
votes
1answer
148 views

Networking structure for Swift iOS app

I would like any constructive comments regarding the structure of this simple App that takes an API response and then displays on a table view. The URL is written in a ConstantsAPI file ...
5
votes
1answer
3k views

Canonical Implementation for a Concurrent Subclass of NSOperation in Swift 2

I would like to develop a kind of template or canonical implementation for a concurrent subclass of NSOperation in Swift. See here for my previous request which is implemented in Objective-C. In that ...
4
votes
2answers
719 views

Big Nerd Ranch Bronze Challenge: Disallow Alphabetic Characters

I just completed BNR's bronze challenge for disallowing alphabetic characters, and would love some feedback on what I did right and wrong, and how I can improve my code for reusability and ...
4
votes
3answers
5k views

Project Euler #8 - Largest product in a series

I just finished Project Euler #8 in Swift, and since there is not any version yet on Code Review, I would like to have some comments on what I did to try to improve it. The four adjacent digits in ...
4
votes
2answers
6k views

Removing a subview with a subview from a UIView without memory leaks

I made a custom UITextView for vertical Mongolian writing. It is made by subclassing UIView, which has a subview called ...
3
votes
2answers
255 views

Sequence-based enumeration of permutations with Heap's algorithm

Heap's algorithm is an algorithm to generate all permutations of a given array. It ... generates each permutation from the previous one by interchanging a single pair of elements; the other n−2 ...
3
votes
1answer
1k views

Basic Caesar Cipher in Swift 3.1

I have been learning Swift for the past day and thought I'd try a basic problem where I can do String manipulation. I am only on pg.100 of the Swift Programming Language on iBooks. Compared to python ...
3
votes
1answer
1k views
3
votes
1answer
239 views

Project Euler #12 in Swift - Highly divisible triangular number

I just finished Project Euler #12 in Swift, and since there is not any version yet on Code Review, I would like to have some comments on what I did to try to improve it. The sequence of triangle ...
3
votes
1answer
202 views

Recursive flattening of Swift sequences - an overly complicated approach

I recently read and answered Martin R's Recursive flattening of Swift sequences and continued to play around with the code until I arrived at something that was both pretty cool and possibly an ...
3
votes
1answer
3k views

Set a font for a UITextView if the font is nil or not equal to desired font

Main question I am setting a font for a UITextView that is a subview in a custom view. I only set it if the user didn't specify the font size in the Interface Builder. I have the following working ...
2
votes
1answer
7k views

AES-128 Encryption/Decryption of NSData

I recently found an Objective-C extension on NSData that encodes and decodes data with AES-128. I made an attempt to port this code to Swift 2.2: NSData+AES.swift ...