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
40
votes
0answers
776 views

Swiftly turning wheels – The May 2017 Community Challenge

This is my attempt at the May 2017 Community Challenge in Swift, with a chain consisting of rigid links. I took this as an opportunity to learn SpriteKit, Apple's framework for 2D games. At least ...
37
votes
4answers
30k views

Swift 1.2 Singleton Implementation

I am pretty new to Swift, as a solo developer I was wondering if somebody could pass comments on the singleton implementation below. The code does work, but being new to Swift and knowing that there ...
29
votes
0answers
858 views

Making a generic NSMapTable replacement written in Swift thread-safe

This is a follow-up to this question. While discussing some details about the code I posted there, I came upon a problem with thread-safety. After searching and trying different things, I reached a ...
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. ...
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 ...
23
votes
9answers
3k views

Is this FizzBuzz Swift-y?

...
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 ...
17
votes
2answers
19k views

Getting WiFi SSID on iOS in Swift

I've seen a lot of Objective-C implementations but I'd like to do it in Swift. I did it like this and it seems to work just fine. Does anyone have comments and/or improvements to make? ...
15
votes
4answers
2k views

Int extension for translating integer to plain English

This code was inspired by this question: Number to Words ...
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. ...
14
votes
3answers
24k views

Swift iOS - Call back functions

I'm working on an iOS app which handles API request based on authentication. Authentication is done through OAuth and every API request is sent along with ...
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 ...
14
votes
2answers
4k views

Let's play some Swift Poker

Before you can do any sort of card game, you must first write some code to define your deck of cards. One thing I've noticed in looking at some of Apple's Swift interfaces is that they very much so ...
14
votes
0answers
226 views

Water flowing swiftly over farmland – The August 2016 Community Challenge

This is my attempt to solve the August 2016 Community Challenge in Swift. I tried to implement the algorithm described by @200_success: Each Cell keeps track ...
12
votes
1answer
2k views

Stack implementation using Swift

I would like to request some code review and feedback on a stack implementation using Swift. Disclaimer: I might have not used protocols, optional unwrapping and error and ...
12
votes
1answer
3k views

Swift string multiplication

I like Ruby's string multiplication so I thought I'd see if I could do it in Swift. I came up with these implementations: ...
12
votes
1answer
328 views

Cat fight (with rockets)

My code is attempting to implement FP in an efficient / readable manner into some of my video games. I realize this may be a bit subjective, but I feel there is enough merit / objectivity to be of ...
11
votes
2answers
552 views

Quickselect algorithm in Swift

I recently answered a question on Stack Overflow about finding the k-largest element in an array, and present my implementation of the Quickselect algorithm in Swift for review. This is essentially ...
11
votes
1answer
302 views

Secret Santa with Groups on Swift

I am working on an special version of Secret Santa with internal sub-groups. Typical Secret Santa (SS) requirements: Cannot be assigned to themselves Should be random Every participant should be ...
11
votes
1answer
4k views

Storing global styles and utilizing them throughout view controllers

Assuming an app has many display styles, fonts, colors, etc., and that we never want to hardcode values, I've created an AppVariables object that houses properties ...
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 \...
10
votes
2answers
2k views

Project Euler #4 (largest palindrome product) in Swift

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. Find the largest palindrome made from the product of two 3-...
10
votes
1answer
2k views

Layered Neural Network in Swift

I am designing a very basic layered neural network in Swift as an exercise. I currently got the network evaluating the response for a given stimulus by propagating the stimulus forwards through the ...
10
votes
1answer
615 views

Parse query checks and increments

In my app, the user can upvote a cycling track if they want to ride on it. Note: I do understand I can use a boolean column to check if a user has voted or not, but, I am just testing at the ...
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 ...
10
votes
1answer
4k views

Beginner stopwatch app

I'm learning Swift and would appreciate any code review suggestions (the more nit-picky, the better). In particular, I'm wondering if there's a more efficient way to format the time in the ...
10
votes
1answer
578 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
1answer
598 views

Networking in iOS Swift

This piece of code is working - It gives me a lot of JSON back, which I have yet to parse, but I'd like to show my code here first to get tips on how to improve what I already have: VenueService....
10
votes
1answer
128 views

Portfolio object made of Asset classes and Securities

I am learning Swift and object oriented programming. I am new to design patterns and protocol oriented programming. In my test code I have several related objects: ...
10
votes
2answers
3k views

First assignment for Stanford iOS8 Course: enhancing a calculator

I recently started learning Swift through Stanford iOS8 Course. The first assignment is to add some features to simple Calculator which was presented during lectures. List of tasks: add button for ...
9
votes
1answer
2k views

Piano app in Swift

I'm making my first app in Swift: a Piano app. I'd like to get your feedback especially about how I can make codes shorter. I maybe have to use arrays and loops and something, and searched by myself ...
9
votes
1answer
3k views

Mocking UserDefaults in Swift

In my application, I am using UserDefaults to save some data. I also started writing tests for testing my application code. At one place I stuck where I wanted to ...
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 ...
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 ...
8
votes
3answers
13k views

Background execution in iOS

The following class BackgroundExecutable is to be used to take a function f and return a function ...
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 ...
8
votes
1answer
1k views

Fibonacci in linear time by using an extra pointer

I have a function to find the nth number in a Fibonacci sequence, in which I am recursively calling the function. The sum is stored in a class variable and I have an extra pointer I increment every ...
8
votes
1answer
1k views

Extending CGPoint to conform to Hashable

For Advent of Code Day 1, I found myself needing to use a tuple as a key in a dictionary. Seeing that I can't extend tuples in Swift, I decided to extend CGPoint ...
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 ...
8
votes
2answers
11k views

Is this a correct use of using protocols and delegate pattern in Swift?

I am trying to grasp the concept of delegates and protocols in Swift. So I have implemented my own PlayableMedia protocol with two concrete classes ...
8
votes
2answers
8k views

Type to byte array conversion in Swift

I need the byte representation of a given type, especially double, float, int8, etc. ...
8
votes
1answer
20k views

Coredata delete all data in an entity

I have this code that I am using to delete all records from an entity. The question is can it be done better or is already the best? ...
8
votes
2answers
2k views

Fraction (rational number) structure with custom operators

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

C hack: replace printf to collect output and return complete string by using a line buffer

I have this great C program I'd like to embed into an iOS app. One passes command line arguments to it and the results are printed to stdout via ...
8
votes
1answer
2k views

Creating SwiftReachability Class in Swift 2.2

I am novice at creating independent classes for user. I have seen many Reachability library for Swift, but I need to create my own class for that. Here is my code: ...
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 ...
8
votes
0answers
1k views

Asynchronous Array.map

I put together an asynchronous map function for Swift's Array using Grand Central Dispatch. It's called with a transform closure,...
7
votes
3answers
1k views

Project Euler, Challenge #5 in Swift

The problem: 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive number that is evenly divisible by all of ...
7
votes
3answers
201 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: ...