Swift is a multi-paradigm, compiled programming language developed by Apple for iOS and OS X development.
2
votes
1answer
65 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
92 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
47 views
Calculating the tip and tax portions for a refund amount
refundableSale is a PMSale object with properties refundableTip, ...
3
votes
1answer
30 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
39 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
88 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
47 views
1
vote
0answers
20 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
66 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
72 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
89 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
86 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
37 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
79 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
77 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
92 views
Swift HackerRank Balanced Brackets
I solved this stack problem in Swift. Looking for any feedback on my code:
...
0
votes
0answers
58 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
106 views
Postponed Prime Sieve in Swift
Motivated by Unbounded Sieve of Eratosthenes in Swift,
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 ...
1
vote
1answer
72 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
83 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
55 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
198 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
83 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
49 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
114 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
81 views
NSFetchedResultsController Container
This is a wrapper for multiple NSFetchedResultsControllers. Essentially, a drop in replacement for NSFetchedResultsController to ...
3
votes
1answer
181 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
38 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
34 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
27 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
174 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
37 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
67 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
72 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
140 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
29 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
104 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
86 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
58 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
153 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
30 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 ...
3
votes
1answer
98 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 ...
2
votes
2answers
198 views
UIButton Subclass - Overriding getters to return placeholder/default values
I'm in the process of converting a custom UIButton from Objective-C to Swift which has custom properties and default values for those properties:
MyButton.h
...
2
votes
3answers
163 views
Calculate the sum of all primes less than 2,000,000 in Swift
This is a Swift program I wrote to calculate the sum of primes below 2 million, but it is tediously slow.
I am curious about what makes it so slow. My theory is that copying the filtered array is ...
4
votes
1answer
67 views
Switch expression function
I made a bunch of functions and an enumeration, that together allow me to write something like a switch expression. It's not as powerful as Swift switch statements, since it's lacking pattern matching ...
3
votes
1answer
56 views
Model for math facts
I've written a model for an app that generates math facts kids have to solve by evaluating the operation and the difficulty you pass to it at initialization.
I don't like how I am repeating the code ...
1
vote
1answer
82 views
Unwrapping optional properties from an optional array of objects
I'm currently working with an SDK where methods return an optional array of objects with optional properties. Only the array itself is optional, it will either be nil or always contain non-optionals. ...
2
votes
1answer
74 views
Show and hide views
I have some buttons in my class that show or hide two views (by setting their alpha property to either 0 or 1) when the user taps on them. I have written a function that uses the ...