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.
660 questions
2
votes
1
answer
128
views
Swift: Find neighbor-elements within an array
I need a function which finds the left and right neighbor-element within an integer-array, based upon a given index. If the given element is 0, then it shall return largest index as left neighbor. ...
5
votes
4
answers
303
views
Swift for CLI tools (traversing directories, reading text files)
To compare ergonomics and performance of some languages for my hobby projects, I have written a simple lines of code counter in Swift, Go, Rust and Python. It scans a given directory tree for source ...
1
vote
0
answers
47
views
Using a ViewModel in a simple SwiftUI screen with a list and API call
I'm building a SwiftUI screen that displays a list of notifications, using a ViewModel to handle state and API calls. The screen is fairly straightforward: it has a header and a paginated notification ...
4
votes
3
answers
375
views
Swift - Process file with key-value pairs
I got to process a simple text-file, which contains key-value pairs. Key-value shall be separated by a tabulator.
Here's the prototype, which I figured out:
...
0
votes
0
answers
51
views
SwiftUI NavigationSplitView three-column layout
For getting familiar with SwiftUI NavigationSplitView, I created a prototype, which implements a three-column view.
Screenshot:
Code (View):
...
1
vote
0
answers
52
views
Class to make UserDefaults get/set easier in Swift
I created the following class to ease UserDefaults get/set within my Swift project.
...
1
vote
0
answers
85
views
SwiftUI: Handling number-inputs via TextField (actually String)
I'm creating a Settings-form in SwiftUI. One of the expected values is a double.
I tinkered with NumberFormatter, but it didn't work well for me.
Moreover I had to find a way making invalid values ...
6
votes
1
answer
216
views
Showing an audio waveform from sample audio data with user interaction for zoom in/out
I want to show an interactive audio waveform like this.
I've extracted the sample data using AVAssetReader. Using this data, I'm drawing a UIBezierPath in a Scrollview's contentView. Currently, when I ...
4
votes
1
answer
116
views
Swift Arrays: Write a rotate-right function
Task:
Write a function which rotates all elements of a given array to the right.
Example: [1, 2, 3] => [3, 1, 2]
My solution:
...
4
votes
2
answers
217
views
Swift String-extension 'countOccurrencesOfChar'
Loosely inspired by Ruby's String.count-method I wrote a similar Swift-method myself as an extension to the String-class. Then I had the idea to use a second parameter with which you can control if ...
2
votes
1
answer
110
views
Swift: 'Working with double-optionals'-exercise
Task: Write a function, which receives an optional array of optional integers and returns a none-optional integer. In case the array isn't nil: Return one randomly selected integer. In case the array ...
4
votes
1
answer
239
views
SwiftUI TimeUnits-Converter App
The given task is to implement a time-units app.
I guess I got the main logic right, but concerning the UI-coding there could be improvements.
Here's my code:
...
3
votes
1
answer
70
views
Swift: Mocking a REST API-request
I have read the last two days through several tutorials about how to mock a HTTP-request against a REST API.
Finally I made a prototype for applying, what I have understood from the tutorials.
Here's ...
3
votes
2
answers
407
views
Write a Swift-function, which computes the Digital Root
Task: Write a function, which computes the Digital Root of a given number n.
Here's the solution, which I developed:
...
3
votes
1
answer
78
views
Lists With Generic Data Types In SwiftUI
I've written a simple list in SwiftUI that accepts a generic data type.
The goal was to make a reusable list where when an item was selected, the list would execute a callback with the selected data.
...
2
votes
1
answer
106
views
Show a list of downloads
I've been working on a Swift project that involves a seedbox app for my NAS, and I would appreciate some feedback on the code structure and especially on the placement of my observer. I've implemented ...
5
votes
1
answer
711
views
Caesar Cipher in Swift (iOS-app)
I have implemented the Caesar Cipher in Swift and incorporated it into an iOS-app.
I guess it's formally correct. Please see the attached images, which show how the usage of the app is meant.
Here's ...
4
votes
3
answers
254
views
Bubble Sort as extension to Swift-Arrays
Task description:
Implement an extension for Array, which sorts the elements using the Bubble Sort-algorithm.
My implementation:
...
2
votes
1
answer
216
views
Swift-function for finding missing Integers in an array of sequential Integers
Task description:
Implement a function, which receives an array of unsorted numbers from 1 to 100. In the array zero or more numbers might be missing. The function shall return an array containing the ...
2
votes
1
answer
455
views
Swift-function: Copy directory recursively
Task description:
Implement a function, which accepts two paths: The path to a source-directory & the path to a target-directory. The function shall return true if the directory has become copied ...
1
vote
1
answer
192
views
Implement the Swift pow(base, exp)-function yourself
Task: Implement a function, which accepts two integers and results the first number raised to the power of the second number. Both numbers have to be positive.
Examples:
The arguments 4 and 3 shall ...
1
vote
1
answer
181
views
Function, which replaces consecutive white-space with a single white-space
Task-description: Implement a function, which returns a string with all consecutive whites-spaces replaced as a single-space.
Example: "a[space][space][space]b[space][space][space]c" shall ...
4
votes
1
answer
94
views
Swift: Fetch XML and check it's well formed
I want to download an XML and check to see if it can be parsed at all, no schema validation.
I was thinking to write two free functions, to separate concerns and wrap them in a third, public API:
<...
4
votes
1
answer
155
views
Is Palindrom-function in Swift
Task is simply to write the classic isPalindrom-function.
Here's my implementation:
...
6
votes
1
answer
527
views
Swift-function, which checks if all letters in a string are unique
The task is writing a function, which checks if a string contains only unique characters. Means: is each character is included only one time.
Here's the solution, I have developed:
...
2
votes
2
answers
371
views
Swift-function, which counts all occurrences of a character within a string
I wrote this function, which (shall) count all occurrences of a specified character within a given string.
...
3
votes
2
answers
143
views
Building a UITableView with fixed number of cells progammatically
I am building this pet project with UIKit. The App's main goal is to keep track of my daily expenses.
The AddExpenseViewController is responsible for capturing user ...
2
votes
0
answers
82
views
Refactoring of this massive ViewController with a UITableView in it
I'm making an application that has a UIViewController with a UITableView in order to show the favorite users that the user of ...
3
votes
1
answer
189
views
Swift - efficient array searching
I am trying to find a way to search an Int array (all numbers are > 0) and identify sequences where the sum of each element is a specific number. One element can ...
4
votes
1
answer
192
views
Queue implemented using two stacks in Swift
I need your expert opinion! In fact, for a Leetcode Problem, I had to code a queue using 2 stacks. It's shown just below and functions well.
It is a pretty simple code with peek, pop, push, etc. ...
4
votes
1
answer
139
views
Finding Highly Composite Numbers in Swift
This code finds highly composite numbers (numbers with more factors than any smaller number) in Swift. I'm not too familiar with Swift, I did this in python but it was too slow so I decided to try it....
2
votes
1
answer
209
views
MVVM pattern in Swift
I've been coding for some time now and since I am working alone I'm not strict in my coding structure approach(bad idea) and was only focused on getting things done which resulted in MVC(MASSIVE-View-...
3
votes
1
answer
158
views
Exceeded time limit on Trie search with Backtracking using Swift
I have been trying to solve Trie related problems over at leet code and came across this interesting problem 211. Design Add and Search Words Data Structure
Here is the question for convenience:
211. ...
5
votes
2
answers
645
views
Leet Code 139 (Word Break) using a trie with recursion
I was attempting to solve LeetCode 139 - Word Break
Given a string s and a dictionary of strings wordDict, return
...
3
votes
0
answers
200
views
Merge two strictly increasing sequences, without duplicates
This work is motivated by Sum of natural numbers below threshold, where several Swift solutions to Project Euler #1 (with arbitrary upper bound) are provided.
One way to think of it is to
start with ...
5
votes
3
answers
332
views
Sum of natural numbers below threshold
I have this code:
...
5
votes
1
answer
136
views
Function for formatting data retrieved from a battery via Bluetooth Low Energy
I have the following function in Swift and am looking to improve the readability. The function takes from the deviceData object which holds maps of data like ...
2
votes
1
answer
218
views
Using Swift Combine's .map function with external variables
I have a motionManager class which uses CMMotionManager() to send rotationRate and attitude values to a publisher in the form of a MotionValueModel.
...
2
votes
1
answer
288
views
API call to list top stories using dependency injection in iOS with MVVM
I'm currently trying to find out what's the best networking architecture for MVVM applications. I couldn't find many resources and decided to go with dependency injection based architecture as per the ...
2
votes
1
answer
221
views
Trie data structure in Swift lang
This is an implementation of a generic Trie DS, in Swift-lang, that is using however, for the sake of a easy example, strings.
It implements two operations, insert and search. So it can insert a text, ...
3
votes
1
answer
992
views
UIImage downscale and compression
getFinalImageData(:UIImage) takes a UIImage and downscales its size dimensions (to 400 ...
3
votes
1
answer
184
views
Simplify wordle logic Swift
I wrote logic for "Wordle" game but I think it's too heavy and not very readable.
Is there any way to improve it?
Simple wordle game with conditions:
1) Green colour - "G" if ...
2
votes
1
answer
391
views
SwiftUI/Swift code for changing item values in an immutable struct
just wanted someone to review my code and perhaps simplify it a bit. Everything is working as expected for the moment but it seems too much code for what i'm trying to achieve. Especially the function ...
1
vote
1
answer
339
views
Finding the winner of a tic-tac-toe game in Swift
Working on the problem Find Winner on a Tic Tac Toe Game, I took several hours longer than an artificial time constraint the site had for mock interview purposes. This leads me to wonder if my ...
1
vote
1
answer
185
views
Checking whether two values are isomorphic in Swift
Working through a book problem
Write a function that accepts two values and returns true if they are isomorphic. That is, each part of the value must map to precisely one other, but that might be ...
1
vote
1
answer
368
views
Finding the longest prefix among words in a string separated by spaces (Swift)
Solving the problem
Write a function that accepts a string of words with a similar prefix,
separated by spaces, and returns the longest substring that prefixes
all words. Sample input and output
The ...
0
votes
1
answer
87
views
Understanding How To Approach A Permutational Word Search
This question is carried over from StackOverflow... First time posting here, so bear with me.
The topic is constructing a search made up of a given string, and a series of letters, where each of the ...
2
votes
2
answers
252
views
Create a custom split method that keeps the delimiters in Swift
I have implemented a custom split method that replicates the original functionality of the collection's split method in Swift and keep the delimiters (element separator). After a lot of trial and ...
1
vote
1
answer
728
views
Best way to do Input Validation on Swift/iOS/UIKit
I have the following form that creates a new "activity":
For which I want to enforce the following requirements:
Name => minimum # of chars: 1; maximum # of chars: 50
Description => ...
1
vote
1
answer
232
views
Bank account concurrency problem Swift
I tried to implement a simple BankAccount class to practice providing a solution to the common Bank Account transaction data race problem. Reference to a description of such a problem can be found ...