Last call to make your voice heard! Our 2022 Developer Survey closes in less than a week. Take survey.

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
1 answer
68 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 ...
user avatar
2 votes
1 answer
54 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 ...
user avatar
1 vote
1 answer
83 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 ...
user avatar
1 vote
1 answer
72 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 ...
user avatar
  • 31
1 vote
1 answer
46 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 ...
user avatar
  • 31
0 votes
0 answers
49 views

Finding all "triplets" of an integer array but in a more optimal manner

I'm practicing the following problem Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0. ...
user avatar
  • 31
0 votes
1 answer
44 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 ...
user avatar
2 votes
2 answers
114 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 ...
user avatar
  • 155
1 vote
1 answer
84 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 => ...
user avatar
  • 155
1 vote
1 answer
68 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 ...
user avatar
  • 165
3 votes
2 answers
227 views

Blocking call to an async function

While migrating to structured concurrency it may be sometimes convenient to temporarily make a blocking call to an async function - e.g.: ...
user avatar
  • 601
0 votes
0 answers
18 views

Swift code for playing a video with different action buttons

This is a ViewController to play a video with some different control buttons. I would like to have feedback on the code structure etc. I am not an expert in Swift, this is a way of learning. ...
user avatar
  • 169
1 vote
1 answer
61 views

Concurrent Operation subclass combining KVO compliance and thread safety

This is a subclass of Operation that attempts to combine the requirements of KVO compliance and thread safety. Do I have the KVO notifications and thread safety ...
user avatar
  • 113
1 vote
0 answers
32 views

Clean way to listen to asynchronous messages

I am currently writing an Engine that runs on a background thread which produces outputs asynchronously. Those outputs are gathered by a Pipe, whith a ...
user avatar
1 vote
1 answer
153 views

Optimize Leetcode 239. Sliding Window Maximum using Swift

LeetCode problem 239. Sliding Window Maximum You are given an array of integers nums, there is a sliding window of size k which ...
user avatar
1 vote
1 answer
71 views

Minimum Window Substring in Swift exceeds LeetCode runtime check

This is a popular question on LeetCode: 76. Minimum Window Substring Given two strings s and t of lengths m and n respectively, return the minimum window substring of s such that every character in ...
user avatar
1 vote
1 answer
25 views

Start / stop animating activityIndicatorView for many dataTaskPublishers

I consume a RestApi with few Combine Publishers and want to visually represent ongoing progress with UIActivityIndicatorView. My current logic uses ...
user avatar
0 votes
1 answer
410 views

Debounce and throttle tasks using Swift Concurrency

There are many debouncer and throttle implementations created using Grand Central Dispatch, and even one built into Combine. I ...
user avatar
  • 73
1 vote
1 answer
618 views

Changing the color of some pixels in an image in Swift

My code is working perfectly fine, except for one-thing. The performance is not up-to the mark. What I am trying to achieve is I have an image with a few circles (each circle has a border with ...
user avatar
  • 113
4 votes
2 answers
133 views

Fetching data closure completion

I started learning MVVM and API data fetching, so I made this code: Function loadDirector in which directorName value is ...
user avatar
  • 41
3 votes
3 answers
335 views

Generate all subsets of a set

The goal of the function below is to generate all subsets of a given set, i.e. a collection of sets that contain all possible combinations of the elements in the set. Note that for my application I do ...
user avatar
2 votes
2 answers
128 views

Initialize Swift class or struct values inline

For a Metal application, I would like to create and initalize texture object globally so it will not have to be optional, and will already be ready to use once ...
user avatar
3 votes
1 answer
96 views

Structure and readability of the code in swift ios

For a job in company I had to do a code assignment. My assignment performs all the required tasks needed to be done. But after one month the company provided me a feedback and said that we have ...
user avatar
3 votes
2 answers
136 views

Determine final player in a counting game

I made a function in which 17 players are counting to number N. Function as a parameter takes a number N and returns which player is going to say N. If current number is divisible by 9 the counting ...
user avatar
  • 31
2 votes
1 answer
102 views

Monte Carlo Tree Search Optimization and Loss Prevention

I'm working on an implementation of Monte Carlo Tree Search in Swift. It's not bad, but it could be better! I'm principally interested in making my algorithm: faster (more iterations/second) ...
user avatar
  • 121
2 votes
0 answers
170 views

Fixed-width non-FIFO DispatchQueue

I have implemented a custom concurrent queue on top of GCD which offers two additional pieces of functionality: Limit the maximum number of concurrently executing tasks. Give manual control over ...
user avatar
1 vote
0 answers
31 views

Subclassed DiffableDataSource

This is a datasource for Achievements that can be toggled to be viewed within a grid or list layout. Thoughts/notes This code was previously duplicated in two different ViewControllers. Should the ...
user avatar
2 votes
1 answer
132 views

Swift code to extract most common page sequences from an Apache server log

So I received the following prompt for a code interview: "Please create an iOS app that downloads a text file from our server, parses the contents, and displays the results. The file is a ...
user avatar
  • 149
0 votes
1 answer
68 views

Add protocols inside the same file as controller

I have a question about something maybe not so much important, but every time when I have to decide, I feel it can write in another way. Imagine I am developing and system about authentication and ...
user avatar
  • 239
0 votes
1 answer
669 views

Is this a good way to download images from json Api?

In my SwiftUI app, I'm trying to get some data from a json Api and display this data along with an image. First I have a function that fetches the data from the Api, then I have another function that ...
user avatar
  • 141
-1 votes
1 answer
205 views

Use of parentheses in Swift [closed]

I am learning Swift and just had a quick question about use of parentheses. The code below runs just fine w/o using parentheses, but it certainly looks a lot cleaner using them in this instance. So my ...
user avatar
  • 9
2 votes
0 answers
731 views

SwiftUI custom ScrollView with snap interval

Due that fact that SwiftUI has poor ScrollView customization and doesn't support snap interval from the box I decided to implement my custom component to support ...
user avatar
3 votes
1 answer
82 views

macOS: resetting balance to center (0.5) using Swift

Due to some bug somewhere, my speaker balance kept moving off-center and it was getting annoying. So I cobbled together the code below (based on this question) which—to my surprise—does compile and ...
user avatar
1 vote
3 answers
951 views

Convert first letter of every word to uppercase

I am learning swift and I came across this problem. Converting each start letter to capitalized form if it's lowercase. ...
user avatar
0 votes
2 answers
141 views

How to load and get data from 2 files

I want to load 2 files and show this data in 2 strings. I trying to do it like this and it works fine. But I think it is not the best solution... How to do more elegant solution? ...
user avatar
  • 1
2 votes
0 answers
137 views

A Daily goals list 'Dopamine Box'

Inspired by Mike Boyd's 'Dopamine Box' video: link, I decided it would be a good project to pursue by myself in swift. The end goal is to make it look like the box in the video, with switches and a ...
user avatar
0 votes
1 answer
168 views

Sqlite select and delete operation using C API in Swift

I've following sqlite code using C api within Swift having a read and a delete operation for review. For those not familiar with Swift, defer will execute after the ...
user avatar
5 votes
2 answers
614 views

Better way to provide data to UIKit component from Combine pipeline

Today I'm researching how to provide data to UIKit component when using Combine pipeline. I could see tons of tutorials about how to use SwiftUI + Combine from internet, but not found anyone helpful ...
user avatar
  • 151
1 vote
1 answer
408 views

Create buttons with MVVM architecture

im trying to migrate my very large swift view controller to mvvm but it still feels very large, can you guys give me any advice What this controller does is simple it shows a UIView on which i can ...
user avatar
4 votes
1 answer
210 views

JSON parsing service

I created the following service for the purpose of sending in a file for parsing. I'm using the generics so that when I call parse() I can pass a custom type ...
user avatar
2 votes
1 answer
160 views

Display a Text() with a foreground color based on a condition

I decided to write a simple tab bar for macOS using Swift. tabs.swift ...
user avatar
3 votes
1 answer
132 views

Swift iOS rewrite a function that use Firebase database without updating a global variable

I'm learning swift programming and i built a music trivia app for iOS that use firebase real time database. I have a function that before the game start get the number of record for each category of ...
user avatar
4 votes
1 answer
98 views

Decoding indeterminate types using JSON Decoder

I'm working with a REST service that responds with indeterminate types. In particular, it can respond with a String, a correct payload (...
user avatar
  • 183
1 vote
1 answer
45 views

"Pick up" images when tapped on and keep track of it

Trying to get my first job as a developer I made an app I was extremely proud of: https://github.com/AnatolyRudenko/Crystals.Dragons The game starts in a random room of a random maze. Some prebuilt ...
user avatar
2 votes
1 answer
75 views

Which is better-way to casting model

I need to convert my model to fit data in tableview cell finding better approach ...
user avatar
2 votes
1 answer
152 views

UIView position updates based on device motion via RxSwift

I've created a simple function to work as a level, where the dot on screen is centered when the device is completely flat. It changes position based on device orientation. I use RxSwift to update the ...
user avatar
  • 101
5 votes
1 answer
304 views

Sorting JSON items from a site

So I recently applied to this job for iOS Developer and was invited to complete the following code challenge: As a next step, please complete this coding exercise in order to proceed with our ...
user avatar
  • 51
2 votes
1 answer
113 views

Projecteuler.net Problem 2 using collection pipeline Pattern

I solve projecteuler.net Problem 2 deferent way Generate number from 1 to range ex 100 and get the even number Get Fibonacci numbers from list Reduce array I have one problem with a large set of ...
user avatar
1 vote
0 answers
315 views

Simple bruteforce algorithm in Swift

So, I am learning Swift and as a little exercise I tried to implement a BruteForce algorithm. That algorithm is pretty simple however it is a bit slow too... Well, I am new to Swift, so maybe some ...
user avatar
  • 111
1 vote
1 answer
191 views

Improving sqlite3 code in swift using c api

Trying to come up with a pattern where the code to open db, writing queries is elegant and clean while handling all possible error conditions. Following code ...
user avatar

1
2 3 4 5
14