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
-4
votes
0answers
17 views

how to pass / update data between UITableViews in swift?

I have 2 ViewControllers, both contain UITablView: What I am trying to achieve is that as follows: It is much like a survey/ choice pages where in InputVC list of possible choices are displayed and ...
-2
votes
0answers
30 views

Should you duplicate a dependency that is used on two use cases in the same class to simplify mocking it in unit test?

So my view model like this: final class ViewModel { let reloadData = reloadDataRelay.asSignal() private let reloadDataRelay = PublishRelay<DataEntity>() let closeSession = ...
1
vote
1answer
80 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 ...
1
vote
0answers
16 views

Firestore implement Notifications View

So to begin with this question is not related to Push Notifications. What I want to achieve is a View in which all notifications are displayed. Something like Instagram or Facebook. There could be a ...
-2
votes
1answer
45 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 ...
2
votes
0answers
45 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 ...
0
votes
0answers
26 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 ...
3
votes
1answer
50 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 ...
-4
votes
3answers
156 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 ...
4
votes
3answers
294 views

How should I go about 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 ...
0
votes
1answer
105 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 ...
3
votes
4answers
267 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 ...
2
votes
2answers
125 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),...
24
votes
3answers
6k 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,...
2
votes
1answer
884 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 ...
2
votes
1answer
50 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 ...
1
vote
0answers
40 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 ...
1
vote
1answer
43 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 ...
0
votes
1answer
37 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....
0
votes
2answers
359 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 ...
0
votes
1answer
105 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) ...
0
votes
1answer
93 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 ...
1
vote
2answers
212 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:...
3
votes
1answer
251 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 ...
1
vote
2answers
220 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....
2
votes
2answers
188 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 ...
5
votes
1answer
282 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. //...
10
votes
3answers
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 ...
5
votes
1answer
443 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 ...
3
votes
1answer
519 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 ...
4
votes
1answer
2k 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 ...
1
vote
3answers
201 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 ...
3
votes
1answer
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 ...
3
votes
1answer
446 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."...
3
votes
3answers
3k 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 { ...
1
vote
0answers
87 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 ...
-5
votes
1answer
137 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?
5
votes
1answer
182 views

Use more dynamic dispatch could reduce compile time?

In swift the compilation time is really slow the amount of code in your project increases. So i was looking for ways to reduce that time. One approach maybe is to use language keywords like final or ...
6
votes
4answers
3k views

Struct vs class in Swift

One of the stated reasons that structs can be more performant than classes is that no ARC is needed for structs. But suppose we have the following struct in swift: struct Point { var x:Float ...
1
vote
2answers
165 views

Are there any downsides to creating a stub by subclassing in Swift?

I'm mainly interested in answers in regards to a Swift language, but more general answers are always welcome. I want to test a class (CUT), and for that I will use dependency injection. Now, I can: ...
2
votes
2answers
335 views

Which one is more efficient; a subclass of UITableViewController for multiple purposes or multiple sub classes each for a purpose?

Say I have two views (Table View for example) that I'd like them to do different stuff; each loads different data but behaviors are similar for most par except what happens when a cell tapped for ...
1
vote
4answers
207 views

When should a function that calculates an object's property be defined within that same class?

As an example below, should the isClient() function be part of the Attendee class? import Foundation ///A Node that can be used with the CriteriaEvaluator to determine if clients are present in a ...
2
votes
1answer
138 views

Correct Design Choice for Maintaining “static” View Across Multiple Controllers?

First, I am not sure if I'm asking this within the correct Stack Exchange community, so if this question belongs somewhere else, please let me know. It didn't seem appropriate for Stack Overflow. I ...
3
votes
2answers
907 views

A function as a parameter in UML

I was wondering what is the best way to represent a function that is passed as a parameter in UML. Because I want to create a sequence diagram of my current networking code in my Swift project and ...
3
votes
1answer
275 views

Is it an anti-pattern for Swift functions that take in a sender to have a label for that parameter?

This is something I see all over Cocoa: func someAction(_ sender: Any) which is called like: someAction(someObject) This can be very confusing to me. The infamous example is in NSView subclasses: ...
5
votes
1answer
2k views

Swift-like extensions in Java using inheritance

After picking up some Swift skills with Java as my strongest language, one feature of Swift that I really like is the ability to add extensions to a class. In Java, a pattern I see very often is ...
1
vote
1answer
544 views

Best approach to learn OpenCV to use with Swift? [closed]

I am trying to figure out how to use OpenCV with XCode's Swift. I need to do realtime video processing for the project I am working on, but I can't seem to find any good references on learning OpenCV ...
0
votes
2answers
1k views

Design for keeping local user profile data in sync with the server

So I'm working on an iOS app and I’m thinking about how to keep user profile data in sync between server and client. The first idea that comes to mind is to create a class called User. It would be a ...
0
votes
2answers
438 views

What's the point of Swift's function notation?

Why does Swift use this function notation: func greet(person: String, day: String) -> String { return "Hello \(person), today is \(day)." } Like, I don't get why it uses the small arrow -> ...
1
vote
1answer
322 views

Design for iOS app backed by API

I was wondering if anyone out there has general tips for how to structure an iOS app backed by a REST api. There are basically two approaches to this that I've thought of API calls placed ad hoc in ...