Questions tagged [swift-language]

Swift is a programming language designed by Apple for creating iOS and OS X apps. Swift builds upon the foundation of C and Objective-C.

Filter by
Sorted by
Tagged with
1 vote
1 answer
98 views

How do I mock API responses with security in mind?

My team and I are beginning to mock our API responses in our iOS app so we don't have to worry about our backend being up when testing. I have conditional compilation directives based on the ...
Derek's user avatar
  • 121
3 votes
4 answers
473 views

When should tuples be used as an argument instead of an array?

I hope this isn't too off-topic/opinion-based, I'm new here. I want to pass three elements of the same type into a function. The three elements are unique cards of a deck of cards, so they're not ...
David's user avatar
  • 49
2 votes
1 answer
140 views

Design for the future or make it tightly coupled to the implementation

My question is regarding how would the developer know when you add interfaces/protocols to their code and inject them as dependency. Consider an example of BudgetService. protocol ...
john doe's user avatar
  • 141
3 votes
1 answer
115 views

Should I have different instances of the same model in an application or just keep a single instance?

What is correct in your opinion regarding the creation and handling of models in an application let's say using MVVM, or even an MVC design pattern? I will try to illustrate the situation I see at my ...
Lucas's user avatar
  • 139
0 votes
2 answers
1k views

Is it okay for an Interface Adapter / Repository / Gateway to use Entities in its implementation

I've got these classes: enum Environment { case staging case production static func current() -> Environment { #if STAGING return .staging #elseif PROD ...
Bawenang Rukmoko Pardian Putra's user avatar
1 vote
1 answer
290 views

SwiftUI View permissions best practice?

I am currently developing an iOS application where there are options on the screen to edit and delete a list. Only the user who created this list can edit or delete it. I am struggling to determine ...
Trenton's user avatar
  • 17
-2 votes
1 answer
107 views

Store/Pass User data MVVM+C swift

I have an app written using MVVM and RxSwift. For navigation I'm using coordinator What is the best way to store user's credentials and pass them to Network Layer. I don't want to sore them in DB or ...
Roma's user avatar
  • 107
2 votes
4 answers
244 views

Swift: Best way to store crypto amounts?

Having a bit of trouble on deciding the best way to store crypto values which are usually in decimals. Do I go with Double or Float? Or is there an even better type that Swift offers? My initial gut ...
Tank12's user avatar
  • 139
1 vote
1 answer
88 views

Guidelines for using extensions in Swift

The other day, I came across this question on StackOverflow. In short, the user who asked the question wanted to extend a class from a third-party library to implement the Codable protocol, but ...
TallChuck's user avatar
  • 142
8 votes
1 answer
7k views

The right place for "app logic" in MVVM context

I hope this is the right place to ask. I'm an experienced developer, and have used MVC for much time. This question is in the context of iOS/macOS development for the most part, (SwiftUI). Using MVC ...
Woodstock's user avatar
  • 191
1 vote
0 answers
38 views

Should I separate a variable that changes often into a different entity although it is contextually should be inside another entity?

So, I have an entity called Consultation that is being used as a member variable of the class VideoCallViewModel that goes like this: struct Consultation: Equatable { enum Status: Int, ...
Bawenang Rukmoko Pardian Putra's user avatar
4 votes
0 answers
1k views

Best practices for viewmodels for SwiftUI preview?

Are there best practices about how to prepare lightweight viewmodels with dummy data, that could be used in SwiftUI previews? Right now, I have a viewmodel for an in-app purchase screen, that needs to ...
Tomáš Kafka's user avatar
0 votes
0 answers
196 views

What would be a good approach to applying computer vision to automatically edit out the downtime in tennis video?

I have an iOS/macOS tennis app that now lets the user import video, and I would like to add the ability to automatically edit out the significant amount of downtime where players are not in a rally or ...
Curious's user avatar
  • 95
-1 votes
1 answer
818 views

Tableview cells repeating containing collection view Swift

I am having trouble presenting my tableview cells containing a Collectionview correctly. I want to display a different animal in each section based on the firebase data. My problem is about every 4 ...
Phillip Cabaniss's user avatar
3 votes
3 answers
2k views

What is a good naming convention for Interface Adapters, Use Cases / Interactors, Entities?

I'm using MVVM + Clean Architecture in my app. And I sometimes don't have any idea if my namings are good enough for each Interface Adapters, Use Cases / Interactors, Entities. For View Models, it's ...
Bawenang Rukmoko Pardian Putra's user avatar
1 vote
2 answers
629 views

Clean Code: How much architecture code should be written if architecture is supposed to be fluid

I watched a video series from Uncle Bob on Clean Code. He makes a few points on architecture which I both agree with but I believe could have clarification. From Uncle Bob: The Interactor is ...
Michael Ozeryansky's user avatar
1 vote
1 answer
101 views

What is better way to track mistakes in error-prone part of a feature?

That is quite specific circumstances I've come across, and I somewhat struggle to find proper way how to approach this. I'm given a class written in swift-language, which has a control property, like ...
The Dreams Wind's user avatar
-2 votes
1 answer
206 views

Best way to deal with lots of nested closures in Swift

I have some external SDK library that makes IO calls (either networking or database) in the form of blocks, like so: SomeClass.doWork(success: {}, failure: {}) Now I need to chain about 60 different ...
zaitsman's user avatar
  • 384
2 votes
0 answers
66 views

What is the idiomatic Swift way to add general functionality via protocol/extension?

I am a scala developer new to swift. In scala we can share implementation across a variety of classes by "extending" a trait that has default implementations for the methods. I would like to see how ...
WestCoastProjects's user avatar
1 vote
0 answers
322 views

Common patterns for Observable data layer on iOS

I am working on learning native iOS development in Swift, and I am trying to find something that is similar to what I've learned in Android development with Kotlin. In particular I am referring to ...
papafe's user avatar
  • 171
3 votes
1 answer
112 views

Need technical advice about passing data through UINavigationController

First of all a small introduction, im relatively new to Swift and to programming in general, been doing it for the last year and loving every and each new thing of this vast world. My post is about ...
ARams's user avatar
  • 33
-4 votes
3 answers
251 views

How to Replace Many if Statements for many types

I have many dependent statements. What is the best approach to handle these cases dynamically? Example enum UserType { case buyer case seller } enum ViewType { case active case ...
a.masri's user avatar
  • 101
6 votes
5 answers
495 views

What is a good approach to naming when modeling a sport that can be between either individuals or teams?

Tennis is played as singles or doubles. I considered making my tennis scoring model logic refer to "teams" throughout its naming since "player" wouldn't take into account doubles. However, seeing ...
Curious's user avatar
  • 95
0 votes
1 answer
126 views

Building native iOS app that runs on iPhone, iPad and Mac

Since Apple introduced Catalyst ealier this year during WWDC '19 it becomes possible to have a single codebase for an app that runs on iPads and Macs. Already for a long time it was possible to ...
Stan Reduta's user avatar
3 votes
4 answers
295 views

Should a transformation function take a nullable and return a nullable or should the caller handle nullability

Often I need to transform a type to another, such as a networking model to a data model, or a data model to a binary representation. Should these transformation functions take an Optional/nullable ...
Michael Ozeryansky's user avatar
2 votes
2 answers
659 views

Localize a countdown timer

I have a very specific localization problem that I have not had any luck finding specific information about in my research. I have an app that I'm localizing that shows elapsed minutes(M), seconds(S),...
TheJeff's user avatar
  • 139
24 votes
3 answers
7k views

Struggling with cyclical dependencies in unit tests

I'm trying to practice TDD, by using it to develop a simple like Bit Vector. I happen to be using Swift, but this is a language-agnostic question. My BitVector is a struct that stores a single UInt64,...
Alexander's user avatar
  • 4,410
4 votes
1 answer
3k views

What is a good approach for saving a Swift model that's a struct containing structs to Core Data?

I'm new to Core Data, and am not sure how to reconcile the need to inherit from NSManagedObject with the fact that my model is a Swift struct composed of structs. I chose to make my a model value type ...
Curious's user avatar
  • 95
2 votes
2 answers
78 views

At what size array in Swift would you consider switching to CoreData?

I've developed a reading tracking app in Swift and for simplicity's sake I used arrays for my model. It stores some data about the books a user reads and a small image of the cover. I save them to ...
M. Chollar's user avatar
1 vote
0 answers
49 views

What is the advantage to inferred types *in Swift*? [duplicate]

As I fool around in Swift I have come to absolutely hate variable declarations. int c = 0 seems easier to read than var c:Int = 0 and more informative than var c = 0, and int some() is radically ...
Maury Markowitz's user avatar
1 vote
1 answer
64 views

Modular Quiz in Swift

I've been working on the architecture of my Apps for a while. In the past I've used a SQLite database which I opened in the AppDelegate (Oh dear), and then created a global variable (really!) to ...
stevenpcurtis's user avatar
0 votes
1 answer
51 views

Only be able to sign into app after existing user send invitation [closed]

I am looking to build an app where the only way a new user can enter the app is if they have been invited by an existing user. Are there any API's, tutorials or resources that can help me achieve this....
iOSHG's user avatar
  • 11
0 votes
2 answers
1k views

Does chaining higher order functions mean worse performance?

For clarification, let's review the following example: Consider that we have the following array of Ints: let array = [1, 2, 3, 4, 5] and we've been asked to generate a new array from array ...
Ahmad F's user avatar
  • 101
0 votes
1 answer
147 views

One-field interfaces

I have an interface with only one title field: protocol Artist { var title: String { get } } (A) Should I pass the whole object as I did here: class Album { func setArtist(_ artist: Artist) ...
kasyanov-ms's user avatar
0 votes
1 answer
135 views

What design pattern would help me make my factory more generic

In my code I have 2 separate login types. I have a factory that decides which one to create based on an enum. Each login type has a different type of credential. Currently my factory method takes ...
dubbeat's user avatar
  • 109
1 vote
2 answers
242 views

What is better approach to implement dependency injection using MVVM Architecture?

I need to use DispatchQueue within controller and pass it as dependency. This is what I have tried so far and worked out two solutions. Need to ask what is better and in common use by other developers:...
Bartłomiej Semańczyk's user avatar
4 votes
1 answer
2k views

Repository that performs API calls internally - DDD

Please note: although my question relates to DDD, I am also interested in this from an architectural and OO design perspective. This question may also be a simple case of CQRS and/or interface ...
James's user avatar
  • 556
1 vote
2 answers
299 views

Reusable component with web service

I am trying to create a reusable component / Cocoa Touch Framework in Swift that would display the current weather based on the user’s location. Right now I cannot decide which approach should I take....
SleepNot's user avatar
  • 121
2 votes
2 answers
332 views

Where to put private computed properties?

In Clean code it's suggested that private helper functions should always exist directly below the function they're directly used within. Should I be doing this when using computed properties? It's the ...
Declan McKenna's user avatar
5 votes
1 answer
316 views

How to break down complex if conditions that include optional binding?

I've recently been reading Clean Code and there was a really nice example in java of when it is beneficial to break down a complex if statement condition in to a function rather than use a comment. //...
Declan McKenna's user avatar
10 votes
3 answers
2k views

Is it good practice to wrap a related set of properties into its own struct/class?

Writing a User object in Swift, though my question relates to any strongly typed language. A User can have a bunch of links (FacebookProfile, InstagramProfile, etc). A few questions around this. Is ...
Prabhu's user avatar
  • 323
5 votes
1 answer
578 views

Dependency Inversion Principle (Swift) - Applicable also without polymorphism? (Abstraction: constrained generics)

There exist a number of articles/blogs explaining the Dependency Inversion Principle (DIP) using Swift; to name a few (top Google hits): GitHub/ochococo/OOD-Principles-In-Swift - The Dependency ...
dfrib's user avatar
  • 201
3 votes
1 answer
763 views

Does swift value type like String, Array, Dictionary internally contain class

I was watching WWDC video and can not understand when they talk about Even though all of these properties are value types, internally, they contain a class which is used to manage the lifetime ...
maddy's user avatar
  • 141
5 votes
1 answer
4k views

Why doesn't Swift allow Int String subscripting and integer ranges directly?

If I have a string: let str = "Hello world" It seems quite reasonable to be able to extract a character: let thirdChar = str[3] However, that's not legal. Instead, I have to use the extremely ...
Duncan C's user avatar
  • 187
1 vote
3 answers
209 views

Should I write out computed vars that all depend on one other var, or should I assign them as normal vars in that single var's setter?

I'm using Swift, but I understand computed properties are a thing in a few other languages as well. I have the following case: var bar in class Foo is changed throughout program flow. vars a, b, c ...
Kevin's user avatar
  • 844
3 votes
1 answer
1k views

What is the best way using swift 4/iOS to handle asynchronous requests eg. to Firebase Database? [closed]

I've been using firebase database and now firestore for a while. I'm prob an intermediate level programmer and still at uni. I'm still a bit unsure re: the best way to handle calls that are occurring ...
Mike's user avatar
  • 141
3 votes
1 answer
573 views

Why doesn't Swift's Numeric protocol inherit from the Comparable protocol?

I created a generic class MyClass<T: Numeric> {...} and got errors in my functions that tried to use > and <, along the lines of "Binary operator '>' cannot be applied to two 'T' operands."...
SaganRitual's user avatar
3 votes
3 answers
4k views

Are nested closures a good thing?

I've been doing Swift development for a while now with Firebase, and I find myself making a lot of code like this: Auth.auth().signIn(with: credential) { (user, error) in if let error = error { ...
Forest Kunecke's user avatar
1 vote
0 answers
135 views

Design choice: Use NSFetchedResultsController and store derived properties or roll my own data source?

I'm writing an iOS app in Swift that calculates a '1 rep maximum' weight using two inputs, weightLifted and repetitions. One of several formulas is used to perform the calculation with these two ...
Jim's user avatar
  • 157
-5 votes
1 answer
140 views

Trying to understand singleton objects in Swift [duplicate]

In Swift, if I have a class with only type properties and methods (everything declared static) would that be considered a singleton object? If not, why?
Joe's user avatar
  • 101