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

learn more… | top users | synonyms

0
votes
1answer
34 views

Extract regex matches with non matching parts

I want to check a string by using multiple search patterns and split the string at every occurrence. I'm using RegEx and it works fine. But I'm worrying about efficiency and performance. ...
0
votes
1answer
22 views

Accessing a UIView's parent UIViewController using the UIResponder chain

I recently found a static C function on Stack Overflow which returns a UIViewController reference from a containing subview using the responder chain: ...
3
votes
1answer
75 views

Swift FloatingPoint rounded to places

I'm playing a bit with extensions for default protocols in Swift 3.0. I would like to implement rounded(toPlaces places: Int) -> Self for ...
3
votes
1answer
35 views

Creating an area graph in iOS

I am trying to create an area graph from the upper and lower limit values. First I created two line graph from those points as: Sample data points are: ...
0
votes
0answers
30 views

View controllers and delegation for a weightlifting app in Swift

I've got several UIViewControllers that are trying to do too much so I'm trying to learn how to clean them up so that they only have code that does UI things (...
0
votes
1answer
36 views

Calling a method only once in an updateScore function in SpriteKit

This works fine, but obviously it's messy and I'm thinking there has to be a better way to do this. Having to constantly remember to flip the bool back is a pain, is there anything else I can do here? ...
1
vote
0answers
46 views

A custom UIView with gradient fill, border, and variable corner radii

Here is a swift port of UIView subclass that I use often in my apps. It allows me to easily set gradients in place of the backgroundColor property, and also makes it very easy to create views that ...
2
votes
1answer
105 views

Signing a String using an identity and trust from a PKCS #12 Bundle

One of the requirements of a third-party SDK I am integrating with is to sign an identifier string with the identity from a p12 certificate. This code loads a p12 certificate from the application's ...
2
votes
1answer
154 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 ...
0
votes
1answer
54 views

Calculating the tip and tax portions for a refund amount

refundableSale is a PMSale object with properties refundableTip, ...
3
votes
1answer
42 views

Chop off trailing newline for Swift's NSAttributedString

I've made this extension but I'm touchy around NSAttributedStrings, keenly aware that Swift refuses to use the traditional notion of integer lengths on Unicode <...
1
vote
1answer
47 views

LeetCode Counting Bits in Swift

I solve this problem in Swift. I am looking for a code review, thanks! Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their ...
3
votes
1answer
98 views

Efficient way in Swift to trim an array

Let's say I have a struct… struct Person { let isMale: Bool let name: String } and an array of Person structs. I want ...
0
votes
0answers
52 views

House cleaning calculator

I have a tableView holding an array of 4 items: ...
1
vote
0answers
30 views

Spawning nodes with random time interval in SpriteKit

I want to spawn objects with random time interval (from 1 to 3 seconds). I've wrote this function: ...
1
vote
1answer
84 views

Proper cellForRowAtIndexPath cell return in Swift

So I have started creating a UITableView with multiple prototype cells. I currently have the below code setup, which works fine ...
3
votes
1answer
74 views

Safe init using failable guarded initializer

I am wondering if that is a safe and proper init, with one failable init guarded and an init. Are the force-unwrap correct in the self.init as I know they are ...
3
votes
2answers
96 views

Registering Pokémon weaknesses

I recently answered a question that had me wondering if I could simplify this below code without creating an extra function: ...
2
votes
0answers
43 views

Adding or editing a child

I have a class which will add or edit a child. So whenever I click on "save," I need to check the child image. If it's an image, then I need to upload to the image server. Once that's done only, I ...
4
votes
2answers
100 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 ...
-1
votes
1answer
42 views

Swift inheritance versus composition

Say I'm using Parse Server and utilising the PFObject class from the iOS SDK. Say I want to have a Chat class. To use inheritance code (design 1): ...
2
votes
0answers
96 views

Hackerrank Simple Text Editor in Swift

I solve this problem in Swift. However, some cases I got terminated due to timeout because the solution isn't efficient enough. I did check the solutions and they are correct. Any tips on making it ...
7
votes
0answers
83 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 ...
3
votes
1answer
139 views

Swift HackerRank Balanced Brackets

I solved this stack problem in Swift. Looking for any feedback on my code: ...
0
votes
0answers
78 views

Swift Hackerrank Maximum Element in a Stack

I solve this problem in Swift. I'm not sure if using an array was the right way to approach it since the problem was on stacks. Any feedback on my code is appreciated: You have an empty sequence, ...
5
votes
2answers
122 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 ...
1
vote
1answer
84 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 ...
1
vote
1answer
110 views

Hackerrank Sparse Arrays in Swift

I solved this problem and was looking for feedback on my code. More efficient, cleaner, more verbose variable names, anything helps. There are N strings. Each string's length is no more than 20 ...
1
vote
1answer
69 views

Concatenating an array of optional NSAttributedStrings

Imagine you had to join together attributed strings with a separator in between them. Which of the following methods would you use? An extension on SequenceType with a function that takes in a ...
1
vote
2answers
358 views

HackerRank Array left rotation using Swift

I solve this problem in Swift. Looking for feedback on my code or a better solution: ...
1
vote
1answer
97 views

Swift Hackerrank Dynamic Array

I've solved this question. Wasn't sure if I was suppose to use a dictionary in this case, as the question was to use a dynamic array, but I didnt know how else to do it. Was wondering if I can get ...
2
votes
1answer
50 views

Class for exposing information with computer properties (getters)

I need some advice on my code structure. I have read that setters and getters break one of the OOP principals, encapsulation, and that we should avoid them if possible but I cannot think of any other ...
2
votes
1answer
132 views

Swift Hackerrank 2D Array

I solved this question in Swift, but was wondering if there is a better way or if my code can be improved: Given a 6x6 2D Array: ...
0
votes
0answers
98 views

NSFetchedResultsController Container

This is a wrapper for multiple NSFetchedResultsControllers. Essentially, a drop in replacement for NSFetchedResultsController to ...
3
votes
1answer
305 views

Hex String to Bytes (NSData)

I'm trying to convert this Objective-C code (originally found in this Stack Overflow question) which turns an NSString into ...
2
votes
0answers
51 views

Core location background update and upload to server

I want to upload a user's location to a server to send them a location-sensitive alert, so the location needs to be fairly accurate: ...
1
vote
0answers
35 views

Correct Swift Model for JSON

I am working on a program that would generate swift classes/ structures for JSON file contents. The link to the application is Json2Model in case you want to contribute directly. I do not know a ...
2
votes
0answers
29 views

didSimulatePhysics to animate a ball, taking 45% of CPU

According to my time profiler this is taking 45% of my cpu usage: have tried to optimize, and someone on stack overflow -- recommended to post here. Time profiler says 99% of the time is spent on the ...
1
vote
0answers
328 views

Expandable tableView cells

What I wanted to achieve is tableView with clickable cells, anytime somebody click a certain cell, another cell with picker, UITextField etc appears below, it's like a dropdown menu. When browsing ...
1
vote
1answer
26 views

Export class instances to a string (for sharing, backup) and importing them

I have a class (Patient) with many instances.. I need a way to share the instances with other users, so I thought of converting the instance into a string and sharing it, then reconverting it into an ...
0
votes
1answer
52 views

Passing an NSManagedObjectContext

I've been experimenting with how to pass my managed object context throughout my application in Core Data and I came across an approach that I'd like reviewed. I've been creating a protocol for all ...
3
votes
1answer
84 views

Queue class with enqueue and dequeue

I am just learning swift and tried to make a queue class with enqueue and dequeue. The node holds the data for each element The enqueue adds an element to the "queue" The dequeue deletes the first ...
3
votes
1answer
83 views

Implementing joinWithSeparator on a Character array

I recently answered a Stack Overflow question about transforming an array of Characters into a String in Swift. Looking at the question and the Swift standard lib, it appears that there is a method <...
3
votes
2answers
144 views

A generic AVL tree with SequenceType, CollectionType and ArrayLiteralConvertible extensions

I'm learning Swift and trying get a good understanding of some of its many features. The following is an AVL tree implementation with extensions that conform to ...
0
votes
0answers
34 views

Saving Core Data records with one-to-many relationship

This is an weightlifting app that allows the user to maintain a log of lifts (LiftEvent) and information about each lift (Lift). I'd like to know if this is an acceptable way to save a 'record' with ...
0
votes
0answers
114 views

Copy-on-write linked list with value semantics

I'm implementing a SinglyLinkedList struct that uses a private Node class in its implementation. (See this Gist.) ...
2
votes
1answer
93 views

Weightlifting calculator

I'm building a simple iOS app using Swift that calculates the One Rep Max weight a user can theoretically lift. User inputs a weight lifted, the number of repetitions and the app uses one of several ...
2
votes
1answer
62 views

Table View Controller for selecting and saving user default preferences in an iOS app

This is the Swift code from one of the view controllers in my app that calculates a one rep maximum weight a person can theoretically lift. This particular view controller presents a list in a ...
1
vote
1answer
249 views

Check if a number is prime

I'm looking for feedback on the following code which checks if a number is prime using Swift 2. ...
1
vote
0answers
31 views

Abstraction of NSNotification that removes the existing observer for a notification when a new observer is added

I ran into a problem in my app where under some conditions, a view controller might be loaded more than once before its deinit (where I was previously removing the observer) is ever called. So the ...