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. ...
1
vote
0
answers
44
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 ...
1
vote
0
answers
50
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
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 ...
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:
...
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 ...
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
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.
...
5
votes
1
answer
710
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 ...
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 ...
1
vote
1
answer
191
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 ...
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:
<...
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:
...
3
votes
2
answers
142
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 ...
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
138
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....
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. ...
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
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
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 ...
3
votes
1
answer
992
views
UIImage downscale and compression
getFinalImageData(:UIImage) takes a UIImage and downscales its size dimensions (to 400 ...
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
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 ...
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 ...
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 => ...