Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

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.

0
votes
1answer
92 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
84 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
118 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:...
4
votes
1answer
113 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
84 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....
0
votes
0answers
38 views

Where to keep the code for new instance of an object in iOS project?

Example: There is a KingDetailViewController class with the following code. // MARK: - New Instance extension KingDetailViewController { class func newInstance() -> ...
2
votes
2answers
143 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
258 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
233 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 ...
2
votes
1answer
414 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 ...
3
votes
1answer
347 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
196 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
837 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 ...
1
vote
1answer
305 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."...
2
votes
3answers
1k 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
69 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
128 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?
3
votes
1answer
147 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 ...
7
votes
4answers
2k 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
132 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
169 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
202 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
121 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
657 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
227 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
1k 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
466 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
834 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
352 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
264 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 ...
5
votes
1answer
148 views

When should bool.boolValue be used?

Recently a co-worker has taken to checking boolean values in the following manner: if boolVar.boolValue { ... } These variable are generally declared explicitly as boolean types either using: ...
2
votes
1answer
349 views

How to avoid repeating a condition in methods that use it differently?

Let's say I have an app that displays details for a person. Each person can have zero or more phone numbers and zero or more notes attached to it. Thus, I have a Core Data entity Person with one-to-...
1
vote
1answer
95 views

What's the answer to my Unit Testing dilemma when setting up mock dependencies?

I am trying to implement VIPER architecture on iOS in Swift. The problem I have is how to set up the object graph. Please ignore the Router and the Entity as it is not relevant to my problem. That ...
9
votes
1answer
2k views

Why does Swift need witness tables?

I'm trying to read up on implementation details of Swift, and one thing I can't nail down are its "witness tables". It looks like they're a separate vtable pointer used for structs. But why would you ...
2
votes
1answer
143 views

What is the preferred way to check if an optional has a value without using it?

Which of these is the ideal way to check if a value exists without caring exactly what its value is? if let _ = flags[.showField] { showField() } if flags[.showField] != nil { showField() } In the ...
9
votes
3answers
365 views

Why does Swift initialise subclass proper fields first?

In language Swift, to initialise an instance, one has to fill in all of the fields of that class, and only then call superconstructor: class Base { var name: String init(name: String) { ...
3
votes
2answers
1k views

Alternatives to the delegation pattern allowing blind messages between components

Delegates and delegation exist to enable passing specific messages to an observer, regardless of that observer's type. In a coordinator-type architecture, where coordinators manage and run the flow of ...
3
votes
3answers
2k views

Is there such a thing as Protocol Oriented Programming?

I am copy/pasting my comment from this article sumarizing a post on reddit: I really don't think "Protocol Orientation" is something new... You've got protocols in Obj-C, you've got Interfaces in ...
2
votes
1answer
306 views

Sorting a Data Set with multiple 'Tie Breaks'

I'm currently working on a software project that requires several different types of sorting mechanisms. I have been searching, to no avail, to find an algorithm to do a sort with tie-breaking. In ...
0
votes
2answers
402 views

NSUserDefaults, Core Data or neither?

Everytime I press a certain button, it should remove a random index from a dictionary and then save that dictionary everytime its modified. I have done this using NSUserDefaults. I have found that ...
2
votes
3answers
336 views

Why doesn't the compiler assume the if statement condition is correct inside it?

First off all: sorry for the title, but I didn't now how to better formulate the meaning of my following question in a single phrase. While I was writing the following Swift code: if errorData....
6
votes
4answers
3k views

JSON API or Plain JSON

There is a debate in my company on whether we should use the JSON API specification or stick to plain JSON for developing APIs that will be consumed by mobile apps, mainly iOS and Android. One ...
0
votes
0answers
99 views

Best architecture for customising the character

I want to customize the game character before starting the level, for example, select what kind of weapon, armor, clothes, etc. There are several components and each component consist of several types....
4
votes
1answer
191 views

Protocol vs Generic

I have a class that uses a type, but it doesn't need to know anything about that type. In Swift, I can code this up one of two ways: protocol Type { } class Class { // use Type } or class ...
3
votes
1answer
656 views

Why closure declaration syntax in Swift is different from function declaration

let closure = { (args) -> ReturnT in ... } v.s. func function(args) -> ReturnT { ... } Why didn't Apple follow principle of Occam's razor and make closure and function declarations to be ...
4
votes
1answer
473 views

What are best practices when it comes to extensions in Swift with reusability in mind?

I like the Swift way of trying to eliminate unintended consequences wherever possible. Whenever there is a change to the language that deprecates something I have been doing for years, it forces me to ...
1
vote
2answers
579 views

What did the reviewer refer to in this code review?

I sent the code for a job application and got the following review: Regarding the Project Structure: Physical separation is absent. Logical separation is present but not up to best practices. Things ...
0
votes
1answer
383 views

iOS: How to switch between prod and non prod REST end points

A noob iOS dev question: How do iOS developers develop their app with non prod REST URL endpoints while then using a prod REST URL endpoint for actual use by customers? I'm not aware of a way you ...
0
votes
1answer
652 views

multiple api calls vs more complex apis

I have been providing a Web API (.NET MVC 5) of an Oracle Database to people who are making a mobile app. I have to admit that I am not very good at it. I have found that I can easily produce Data ...