Swift is a multi-paradigm, compiled programming language developed by Apple for iOS and OS X development.
0
votes
0answers
38 views
1
vote
0answers
13 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
36 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
65 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
81 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
78 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
32 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
61 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
71 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
76 views
Swift HackerRank Balanced Brackets
I solved this stack problem in Swift. Looking for any feedback on my code:
...
0
votes
0answers
48 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
85 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
58 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
59 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
50 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
127 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
66 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
99 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
72 views
NSFetchedResultsController Container
This is a wrapper for multiple NSFetchedResultsControllers. Essentially, a drop in replacement for NSFetchedResultsController to ...
3
votes
1answer
102 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
29 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
32 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
25 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
103 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
24 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
31 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
58 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
67 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
131 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
26 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
90 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
84 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
54 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
105 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
29 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
96 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
187 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
160 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
65 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
54 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
58 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
71 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 ...
6
votes
1answer
166 views
A* Generic Implementation in Swift
My first Code Review post.
I'm up to a generic implementation of the A* search algorithm in Swift (for now, it's a single goal implementation).
Here's what's been coded so far:
...
3
votes
2answers
106 views
Avoiding misuse of optional unwrapping: LinkedList
Below is my implementation of a generic linked list in swift. Unfortunately, Swift playgrounds won't run until the compiler is totally happy - often, that involves clang making me explicitly unwrap ...
1
vote
0answers
78 views
Showing Or Hiding AdMob Banners - Purchased vs Not-Purchased IAP Banner Non-Consumable
I have the following code that displays a standard AdMob banner depending on whether the user has purchased an ad-free (banner-free) non-consumable In-App Purchase or not (i.e. If purchased, no ...
1
vote
1answer
189 views
iOS MQTT client app base view controller
I've been working on an iOS IOT client app which uses MQTT.
After some reading I decided to go with CocoaMQTT.
I have only been programming in swift/iOS for about 2 weeks now, so I thought I should ...
2
votes
1answer
91 views
Traversing a Binary Search Tree in Swift
I'm implementing the Binary Search Tree data structure in Swift. It looks like this:
...
1
vote
0answers
117 views
Class Level Properties in Objective-C and Swift Classes
I was recently tasked with architecting a user/profile object in Objective-C and wanted the ability to access a static instance from the class level, similar in style to the way Parse manages their ...