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.

115 questions with no upvoted or accepted answers
Filter by
Sorted by
Tagged with
34 votes
0 answers
1k views

Making a generic NSMapTable replacement written in Swift thread-safe

This is a follow-up to this question. While discussing some details about the code I posted there, I came upon a problem with thread-safety. After searching and trying different things, I reached a ...
user avatar
  • 551
6 votes
0 answers
8k 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 ...
user avatar
  • 93
6 votes
0 answers
667 views

Swift class to check ApplePencil reachability

I've implemented a class that lets you check out whether there is an ApplePencil connected to the device or not. ...
user avatar
5 votes
1 answer
358 views

Decoding a JSON dictionary into different Swift classes using Decodable and JSONDecoder

Generally, the approach I've taken is to try calling container.decode from a decodable enum with its associated value returning ...
user avatar
  • 191
5 votes
0 answers
86 views

Moves box in direction of touch, and has wraparound when reaching edge of screen

Part of me thinks that it would be simpler to ditch Apple's move API and just updated position with a timer. I know that's what I did with a JS canvas snake game I built a while ago. But I'm also ...
user avatar
  • 151
5 votes
0 answers
2k views

Reducing complexity in NSView event handling code in Swift

My Cocoa event code always tends to descend into madness, I need to advice on how to reduce complexity. I've spend a few hours putting this example together. CanvasView is a layer hosting subclass of ...
user avatar
4 votes
1 answer
221 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
4 votes
0 answers
52 views

Using UnsafeMutableRawPointers with Metal buffers in Swift

I've some Metal buffers which I populate with geometry data. Most data consists of Floats, but the buffer also contains custom structs for the tessellation stage. I ...
user avatar
4 votes
0 answers
125 views

Swift iOS app for showing motivational quotes, with multiple screens

I'm one month into self-learning Swift, and I would like some advice to improve my coding skills. I have listed 2 situations that I think I need to improve. I'd like some feedback focusing on my use ...
user avatar
  • 61
4 votes
0 answers
669 views

Update UI once all API calls are done

I'm learning Swift at the moment but I would like to produce well-organised code. The goal of my little app is to display the weather of my current location. I use : the API of Darksky : https://...
user avatar
4 votes
0 answers
88 views

Swift framework that facilitates making atomic changes to several objects at a time

For the purposes of an app I work on I have created a separate framework that abstracts away the mechanism of making atomic changes to the complex object structures. Each class that has to ...
user avatar
4 votes
0 answers
369 views

Unit Testing Against Exceptions

I am writing a Swift library. I'm writing unit tests for that library. I am afraid that some of the code that I am testing will possibly throw exceptions. In Swift, we can handle errors, but we can ...
user avatar
  • 25.2k
4 votes
0 answers
252 views

A general iterator to replace "non-standard" C-style for loops

Since the deprecation of C-style for loops in Swift 2.2 (and the removal in Swift 3), several questions about the possible replacements were asked on Stack Overflow. In most cases, a ...
user avatar
  • 22.4k
3 votes
0 answers
474 views

Navigation strategies for a SwiftUI-based app using MVVM pattern

I'm finding it difficult to decide on the best architectural pattern for a SwiftUI app but, for the moment, I'm sticking with MVVM as I found this a good fit conceptually. I'm trying to avoid the ...
user avatar
3 votes
1 answer
69 views

Granular vs a aggregated tests when dealing with arrays

I write code in a Test-Driven way and I often build my functions starting with tests for the easy edge cases. For example, given a flat array of Items that have a <...
user avatar
  • 131
3 votes
0 answers
225 views

Combine asynchronous return values in SwiftUI

I have 2 asynchronous return values from 2 different classes, one from HealthKit, the other from MotionManager. I combine the outcome of these classes through a combinedViewModel. The code works, but ...
user avatar
3 votes
0 answers
81 views

Probability shooting game

I've been working on this probability game that has a gun with 5/6 bullets. When there's a dodge the game removes a bullet and then the game continues, the goal being to get to 0 bullets. I don't ...
user avatar
3 votes
0 answers
1k views

Applying Gaussian blur to UILabel

Here is the code and it is really slow, like seconds slow to render about 25 labels. ...
user avatar
  • 131
3 votes
0 answers
115 views

Load data from WebAPI to the ViewController

My current code working perfectly, it is for data load from web api to the UIViewController (UITableView). But I am confused ...
user avatar
3 votes
0 answers
436 views

Posting iOS notifications from sender or from receiver

In Swift, should I be posting notifications to the iOS notification center from the device sending a text message or from the device receiving it? I'm using ...
user avatar
3 votes
0 answers
2k views

Generic wrapper for Alamofire Request's function jsonResponse wrapper

I am trying to wrap a responseJSON function of a Request class of Alamofire because when I send an API request I get a response ...
user avatar
  • 131
3 votes
0 answers
160 views

Adding swipe support to multiple classes

I have implemented swipe functionality using UIPanGestureRecognizer in a project. What I have done is added PanGesture to ...
user avatar
3 votes
0 answers
208 views

Using the visitor pattern to compose notifications

I'm solving the following problem using the Visitor design pattern in Swift: When a quote is requested or an appointment is booked we send out an email to the customer, and build an internal ...
user avatar
3 votes
0 answers
3k views

Custom view for data input (in-app (non-system) keyboard)

I have made a custom keyboard that gets loaded from an .xib file. I decided to go with an in-app only keyboard rather than a system-wide keyboard because the documentation states: Make sure a ...
user avatar
  • 405
3 votes
0 answers
6k views

Multiple View Controllers in a Single Window (Swift on OS X)

As an exercise I want to have a single window application (in Swift on Mac OS X) with 4 views, each controlled by an independent view controller and have their own XIB file. I have working code, but I ...
user avatar
3 votes
0 answers
412 views

"Extending" the Swift 2 ErrorType protocol

I am building a simple REST client in Swift 2. The methods the client offers can return errors. Every method returns the same errors (network problem, unauthorized, etc...). But each method can also ...
user avatar
3 votes
0 answers
123 views

Second attempt at Sierpinski Triangle App

I am a Java programmer trying to learn the ways of Swift. A few days ago, I posted the code for a very simple app that draws a Sierpinski triangle and implements a pretty buggy zoom. I was told to ...
user avatar
  • 721
2 votes
0 answers
52 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 ...
user avatar
2 votes
1 answer
114 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
176 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
2 votes
1 answer
136 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
2 votes
0 answers
778 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
2 votes
0 answers
140 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
2 votes
0 answers
183 views

Swift Struct-based Factory Pattern

Edit: I added another configuration vector to CardView, a size attribute, which may be .small or ...
user avatar
2 votes
0 answers
51 views

Idiomatic way to mutate array while using Swift dispatch groups

When keeping track of various pieces of information in a variable when using a dispatch group, I've fallen into a pattern of protecting the variable using a serial queue, and then ultimately notifying ...
user avatar
  • 121
2 votes
0 answers
109 views

Convenient way to make an UIImageView ExpressibleByStringLiteral

I want to reduce boiler-plate code and write like this when developing in iOS: let imgView: UIImageView = "share_fareware_text_bg" Here is implementation code: <...
user avatar
2 votes
0 answers
107 views

Swift MVVM: Repositories - loading entities and dependency injection

I create a lexikon and a navigation for ZOO. I use MVVM architecture supported by ReactiveSwift. I have a few questions about my practises and whether I could write my repositories better and more ...
user avatar
2 votes
0 answers
26 views

Simplifying a firestore querying function

I have this function where it queries through the database and returns a random user if the requirement is met if not then try it again. It works and all but I feel like the code could be better/...
user avatar
2 votes
0 answers
556 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 ...
user avatar
  • 93
2 votes
0 answers
109 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 ...
user avatar
2 votes
0 answers
116 views

Listening to sensor data and logging its output

I am creating a Mac OS X application. This application will connect to a MetaWear sensor over ble, and log the accelerometer and gyro data (I want to store 30 samples for each, per second). Here are ...
user avatar
  • 121
2 votes
0 answers
184 views

Comments on singleton Swift status bar alert

I was trying out some projects like a status bar alert. It made most sense for this status bar to be a singleton: there is only one alert visible at a time. ...
user avatar
  • 265
2 votes
0 answers
63 views

Choosing structure of fields validation module in Swift

I've implemented abstract module for fields validation according to this MVP clean architecture repo. In a nutshell I've created ValidationGateway protocol ...
user avatar
  • 211
2 votes
0 answers
874 views

REST API wrapper class with Alamofire + Gloss

I have come up with this structure to handle API calls using Alamofire + Gloss. The multipart method I came up with was kind of in a hurry, it works but I think it could be done much better. So this ...
user avatar
2 votes
0 answers
114 views

Private contexts and their parents in CoreData

I find myself following this pattern a lot in my core data code, which works, but I'm wondering if it is needed... ...
user avatar
  • 121
2 votes
0 answers
331 views

Find MIDI devices with certain names and make them usable

I'm working on a Swift app for macOS, where I take input from MIDI devices and transform that input. The MIDI devices being supported come in for flavors. What my code does is scan for MIDI devices, ...
user avatar
2 votes
0 answers
42 views

Comments on a 'PullableView'

I created a view called PullView which adds a level of interactivity to UI components. PullViews can be pulled sort of like a ...
user avatar
2 votes
0 answers
1k views

Converting [UInt32] -> [UInt8] -> [[UInt8]]

I am trying to speed up my current implementation of a function that converts [UInt32] to a [UInt8] which in turn is split up ...
user avatar
  • 21
2 votes
0 answers
1k views

Implementation of a like button in iOS for Instagram like app in Swift

I have just completed an implementation a like button for an Instagram-like app, but now that I am looking at it I feel that there is something off. Maybe it is my formatting or maybe I am not quite ...
user avatar
2 votes
0 answers
53 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 ...
user avatar
  • 121