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.

-2
votes
0answers
9 views

How to symbolicate the log of an iOS app that contains my framework?

My framework is integrated inside third party iOS app and they complain that their app crashes sometimes because of my framework. Of course, I want to investigate this issue, but the crash that they ...
1
vote
1answer
23 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 ...
0
votes
0answers
27 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
32 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
33 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
134 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
95 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
86 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
134 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
126 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
116 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
152 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
262 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
266 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
421 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
469 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
864 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
330 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
2k 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
71 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
129 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
152 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
136 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
187 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
126 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
699 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
240 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
486 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
877 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
365 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
279 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
149 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
379 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
103 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 ...
10
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
145 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
387 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
337 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
420 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
338 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 ...
4
votes
1answer
200 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
685 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 ...