Questions tagged [swift]

Swift is a general-purpose, multi-paradigm, compiled programming language developed by Apple Inc. for iOS, iPadOS, macOS, watchOS, tvOS, Linux, and z/OS.

Filter by
Sorted by
Tagged with
3
votes
2answers
366 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 ...
5
votes
1answer
4k views

Swift HackerRank Balanced Brackets

I solved this stack problem in Swift. Looking for any feedback on my code: ...
0
votes
1answer
60 views

Change level button's image

There are 12 buttons, and each of them has 3 options for selecting pictures, depending on the data. It works but I think need a shorter and more intelligent decision. ...
1
vote
0answers
402 views

Swift NMEA parser

I'm quite new to Swift, and although I've been programming as a hobby for many years, I'm still not very confident about my code. I'm currently rewriting an old library of mine, an NMEA parser. NMEA ...
1
vote
0answers
69 views

Side Scrolling App Too Slow (using UIScrollView)

I'm building an app where the user can press play and then the app will scroll through some musical notes (like sheet music). I use a timer and counter to move the view along. The code works ok, ...
0
votes
2answers
212 views

Design a system for creating a coffeeshop menu

During an interview I was asked to design the menu for a coffee shop employee so that the employee would go and add different items to the menu. This is the design that I came up with. I just want ...
1
vote
0answers
75 views

Design a system for renting digital movies/books

During an interview I was asked to do an OO design for a digital renting system for books/movies. This is the design that I came up with. My assumptions: When the customer has downloaded a list of ...
0
votes
1answer
36 views

Swift append an array when it's been declared as Any

I'm learning Swift and I have a following problem: I create a dictionary containing some data, one of which is stored as an array. I want make an operation on this array to add some info. Currently I ...
2
votes
1answer
176 views

Finding valid path on 2D Grid

I got a 2D grid (n x n matrix). The user selects his player in this 2D grid and after that selects another node in this grid to mark his destination position. The player is allowed to move ...
2
votes
1answer
96 views

How can this swift, firebase image fetching, function be made more concise?

I am trying to fetch a group of images within a place in Firebase database ref. I then loop through each image adding it to an array which is then supposed to be added to a tableview cell. I was ...
4
votes
2answers
14k views

Basic calculator in Swift 3

I just started learning the basics to Swift 3 about 2 days ago. Since then, I've decided to make a simple calculator that can only do one operation at a time. I was wondering how efficient this was ...
2
votes
0answers
410 views

Swift Throttler and Debouncer

I have a Throttler and Debouncer functions that I took from an unmaintained library. I added my own tests, but not sure if the ...
2
votes
1answer
320 views

Stack using Linked List in Swift

I made this implementation of a stack in Swift that stores its elements as a linked list. It seems to work perfectly well, but I’m wondering how I can improve it to follow the best practices of the ...
1
vote
1answer
488 views

Caesar encryption and decryption

For a small project, I had to implement Caesar encryption in Swift(4). I'm mainly looking for performance optimizations. This whole code should be copy and pasteable in a playground and is ...
1
vote
2answers
135 views

Converting a String to USD

I'm working on a clientside iOS app for my school that involves students purchasing things, and I wrote a String extension for formatting a ...
1
vote
0answers
28 views

Reducing force unwraps in CLI utility

This is a CLI utility for managing the allocation of investment. It's an exercise, don't take it too seriously. So the utility asks the user a series of questions about it's finances and suggests a ...
2
votes
1answer
194 views

Manage Game Level with enum

Basically What I have is basket ball App. In which I have Added some levels and difficulties. logic is simply use tupple in enum and I have some methods to update / get current level with new values ...
5
votes
1answer
3k views

Implementing the Hashable Protocol in Swift with the DJB hash function

A while back I made a custom String struct (see github repo) because of the difficulties in dealing with Mongolian Unicode rendering when using Swift String or <...
1
vote
1answer
91 views

Emit different text and sound when each of three buttons is pressed

I would like to use this same code on multiple pages of photos (to help my son recognize people). So instead of reinserting the name of each person, I created the ...
2
votes
1answer
635 views

Set a UITableViewAccessoryType in a single UITableViewController used for two different purposes

I have a UITableViewController that displays a list of lift names (think weightlifting) and it's used for two different things. It can be accessed from view A to select a lift that will be saved as a ...
2
votes
2answers
137 views

Finding a word that represents an image

This is an app wherein you have to find the word that represents the image. It's the same concept as the course app but I did this one myself. How can I improve this code (put more commentary, put ...
3
votes
1answer
1k views

Swift - Finding longest binary gap of a given integer

I recently had the chance to try Codility's algorithm training, and the very first one in the list is finding the longest binary gap of a given integer. I tried to implement this in Swift, and after ...
3
votes
1answer
849 views

Presenting and passing data to a modal view controller without using prepare(for:sender:) method

I am using a toolbar button to present a modal view controller (in which I let the user export data as a PDF file). The main section of my app is a ...
2
votes
1answer
302 views

Handling several types of messages over a web socket connection

I have a UIViewController with a Socket Connection established once viewDidLoad. This web socket is a multi-directional ...
3
votes
1answer
87 views

Dividing an arbitrary dollar amount into the fewest bills and coins

I am taking a class in mobile application development this fall. I have finished my first assignment of creating a class to divide an arbitrary dollar amount into the minimum number of bills and coins....
1
vote
1answer
109 views

Swift UITableViewCell class to show cameras, ratings for a car wash and other information

I have an UITableViewCell class that written in Swift. And it seems to me that I did it the wrong way. It works fine, but I want improve the code or refactor it somehow to make it look better. Any ...
4
votes
1answer
2k views

A consumable byte array, in Swift

For decoding binary data (in my case, delivered by a Bluetooth device), I've written this struct: ...
4
votes
1answer
135 views

“What To Do” App

I am starting to build up my portfolio, so I am starting to build projects whenever an idea pops up in my head. So, I decided to build an app that helps you decide what to do. Basically how it works ...
7
votes
1answer
406 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 ...
11
votes
2answers
3k 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 \...
3
votes
1answer
276 views

Custom caching class in Swift

I'm using Swift 4.0 and I'm using UserDefaults in my project for storing Strings like login or something. I have a class: ...
4
votes
1answer
608 views

A helper class for creating icons from a PNG image and colour in Swift

I have made a helper struct in Swift that creates circular icons from a PNG image (a white icon on transparent background) and a background ...
24
votes
9answers
3k views
5
votes
0answers
7k views

Page and center UICollectionView like App Store

I need a collection view to page through cells and center it like the App Store, where a portion of the previous and next cells look like this: The native ...
4
votes
2answers
2k views

Swift solution to Leetcode “Longest Substring Without Repeating Characters”

From LeetCode medium 3. Longest Substring Without Repeating Characters: Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", ...
2
votes
1answer
76 views

UITableViewDataSource with different sections and SearchController

I have a UIViewController holding one UITableView with 3 sections. Also this UIViewController implements ...
2
votes
1answer
2k views

Filtering input characters for a numeric text field using logical operators

I did my code for "Bronze Challenge: Disallow Alphabetic characters" exercise in "iOS programming" book by Big Nerd Ranch (p.85). When I did the exercise I also changed code of func textField which ...
1
vote
1answer
2k views

Numbers as Strings Multiplication Function in Swift

I was solving a problem 20, Factorial Digit Sum, on Project Euler. Factorial Digits Sum \$n!\$ means \$n \cdot (n − 1) \cdot \ldots \cdot 3 \cdot 2 \cdot 1\$ For example, \$10! = 10 \cdot ...
4
votes
2answers
207 views

Initializing an Array of Alternating Values

My goal in this project is to make something that can—as, quickly, effectively, and efficiently as Array(repeating: , count: )—initialize an ...
1
vote
2answers
214 views

Checking for required text fields for login

I wrote this script and it's working quite well however I think there's too much if else statements on it. How could I change them for a switch statement? ...
2
votes
1answer
2k views

iOS Memory Game - Swift 4

I'm learning iOS through this past Stanford CS193P course. I've just completed the first assignment and would love a code review as I can't actually turn in an assignment for feedback since the course ...
3
votes
1answer
220 views

Multilingual app without localizing

I'm making an iOS app which supports multiple languages but not in the built-in localized way. In iOS, for localization to work, the user has to change the device's language. But in the app I'm ...
8
votes
2answers
474 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 ...
4
votes
1answer
823 views

Maintainable networking layer architecture

There are so many different solutions and examples on how to build a proper networking layer, but every app has different constraints, and design decisions are made based off trade-offs, leaving me ...
1
vote
1answer
461 views

Core-data object with relationships conforming protocol in swift

I have a situation where I have to use protocol to be conformed by NSManagedObject which has relationships with other entities. My protocol is like: ...
5
votes
1answer
1k 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 ...
2
votes
0answers
108 views

Log in user through json and display the application

I am currently learning swift 4 and would like to ask you to correct my code. (I would like to have a pattern for learning the "beautiful code"). My code gets data from the server in JSON format and ...
6
votes
1answer
88 views

Downset calculation code

I'm looking to port a Python script I have over to Swift, partly for some performance benefits, and partly to dip my toe into a new language. My program computes what is called the downset of a ...
4
votes
1answer
248 views

iOS Swift 3 app that requests data from the Stack Exchange API

This is my first Swift app, so I want to improve everything that I can about my code and know what I can do better. The app requests 10 developers from StackExchange API and displays them in a table ...
7
votes
1answer
10k views

Grouping Date Objects by year and month

I have an array of date objects, which I want to group by year and month. The data structure I have chosen for my target is an array of arrays of Date objects. Two Date objects should be in the same ...

1
3 4
5
6 7
13