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
1
vote
1answer
126 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 ...
0
votes
1answer
230 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 ...
3
votes
3answers
278 views

Efficient way of flat mapping a range of a multidimensional random access collection

I've recently answered a question about reading elements from an array of arrays. A way it could be interpreted is that the OP wanted to read a range that could span over multiple subarrays of the 2D ...
2
votes
0answers
32 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 modified with fetched data: ...
0
votes
1answer
137 views

Is my code Declarative (RxSwift) ? Please help me to explain why not?

I'm newbie in Swift and Rx programming. Is my below code is declarative ? If not, please rewrite it and explain to help me more understanding about declarative :( (my English is so bad) ...
1
vote
0answers
31 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 ...
4
votes
2answers
191 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 ...
1
vote
0answers
14 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 ...
3
votes
1answer
72 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 ...
2
votes
0answers
43 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 ...
3
votes
2answers
2k views

Working with Arrays in Swift

It's an exercise from a Swift-course I'm currently taking. You are given an array of integers. Make an additional array named computedNumbers. computedNumbers should multiply each element in the ...
2
votes
1answer
87 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) ...
3
votes
2answers
495 views

Using enum to create, subscript and loop an array of structs in Swift 5.0

I'm trying to "build" a few "spaceships" in my code. I'm feeling it's a good idea to enumerate all the spaceships and use that as a subscript to build and use an array that holds the spaceships' ...
0
votes
2answers
109 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? ...
2
votes
1answer
56 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 ...
5
votes
1answer
161 views

How to properly decode 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 ...
2
votes
1answer
74 views

Which is better-way to casting model

I need to convert my model to fit data in tableview cell finding better approach ...
3
votes
1answer
57 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 <...
4
votes
1answer
155 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 ...
0
votes
0answers
104 views

iOS: Combine and Clean Architecture

I’m learning a lot of from Combine thanks to an amazing book in raywenderlich. Currently i’m writing an app and following the clean MVVM architecture. Then I have the following layers: Domain(...
2
votes
1answer
116 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 ...
6
votes
2answers
7k views

Countdown timer in every table cell

I am making countdown app with multiple records, so I am using UITableView to show that records and countdown dates. What I do is just take one timer and reload cells on every second. Is this a best ...
2
votes
2answers
141 views

Searching within a data model using an array matching multiple terms

I have a standard tableview app that displays information from a data model array that uses a custom class. There is a UISearchController to filter the data. A ...
2
votes
0answers
118 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 ...
1
vote
0answers
21 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 ...
0
votes
1answer
64 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 ...
3
votes
0answers
298 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 ...
2
votes
0answers
367 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 ...
-1
votes
2answers
109 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 ...
3
votes
1answer
983 views

MVVM in Swift iOS

I've been implementing MVVM in Swift. I've looked at several implementations, many of which violate some aspects of MVVM and wanted to have a go with my own version that contains a Web request service....
3
votes
1answer
363 views

Minimum window substring - LeetCode challenge

Below is my code for the “Minimum Window Substring” LeetCode problem in Swift: Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(...
1
vote
3answers
230 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. ...
9
votes
1answer
22k views

Pure Swift solution for socket programming

I have spend quite some time now to find out how exactly to do socket programming in Swift (Xcode 6.1). There are a few examples out there, but most of them no longer work for the latest release of ...
1
vote
1answer
113 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 ...
0
votes
1answer
99 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 ...
2
votes
0answers
105 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 ...
2
votes
2answers
114 views

Convert C to Swift

I trying to convert this C code for calculating distance from RSSI to Swift code. I try to do it by myself, but considering I'm beginner, I need help in how to do it. Here is C code: ...
2
votes
1answer
89 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 ...
1
vote
1answer
64 views

iOS: prettify random color for debugging

iOS: prettify random color for debugging I used to use random color to debug, from colors I like ...
4
votes
3answers
19k views

Showing activity indicator (loading image) while processing in background

I have a login screen on my iOS app. When I click login button I want to make sure that user should be displayed a loading image in center of the screen. I found lots of different methods online but ...
10
votes
2answers
3k views

Extending CGPoint to conform to Hashable

For Advent of Code Day 1, I found myself needing to use a tuple as a key in a dictionary. Seeing that I can't extend tuples in Swift, I decided to extend CGPoint ...
2
votes
1answer
115 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 ...
49
votes
0answers
1k views

Swiftly turning wheels – The May 2017 Community Challenge

This is my attempt at the May 2017 Community Challenge in Swift, with a chain consisting of rigid links. I took this as an opportunity to learn SpriteKit, Apple's framework for 2D games. At least ...
4
votes
1answer
96 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 (...
3
votes
1answer
92 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 ...
5
votes
1answer
352 views

Using Grand Central Dispatch to fetch data from Parse.com and update UI asynchronously

This works, and the UI is snappy in the simulator, but since this is my first time really using GCD, I'd just like this code to be reviewed before I start using it everywhere. Note that this is ...
2
votes
1answer
354 views

Retrieving creation date from Parse.com data

I got to show the createdAt date in a message timeline, I tried to make a separate array only for dates, but don't know if it is the right path to follow. It seems ...
2
votes
0answers
419 views

Adding a constraint to a PFQuery on a PFRelation with objects that have already been gotten

For some background, I am learning from the following Stack Overflow question that I asked regarding creating the this code. I was playing around with different combinations of ...
3
votes
1answer
99 views

Check if objects have coordinates

Here's a function: ...
8
votes
1answer
9k views

Star Rating in Swift

So I needed a a star rating feature to appear in my app - it doesn't need to be interactive. I have my app connected to a database I've set up in Parse, and I've made a column for OurRatingof type ...

1
2 3 4 5
13