Questions tagged [swift-extensions]

Extensions add new functionality to an existing class, structure, or enumeration type.

Apple Pre-release Docs

394 questions
2
votes
1 answer

How can I add a required property to a subclass that has to implement a required initializer?

Here's the scenario. I have three classes: A, B, and C. Class A is a subclass of B. Class C needs to pass one of its instance variables to class A (let's call it b). I want to make sure that it happens when C initializes A to make sure that I won't…
Alex Bardasu
  • 348
  • 1
  • 8
2
votes
0 answers

Swift convenience init crashes

Here is the code I have in a playground import UIKit extension CIFilter { convenience init(name: String, dummy: String) { self.init(name: name) } } let filter = CIFilter(name: "CIGaussianBlur", dummy: "dummy") It crashes (exception)…
Pierre Marty
  • 166
  • 1
  • 7
2
votes
1 answer

Swift array subscript extension "Range is not convertible to Int"

I'm defining an extension on Array to override Slice creation: struct S { private var array: [T] = [] private var first = 0 private var len = 0 init(_ array: [T], _ range: Range? = nil) { self.array = array …
ßßß
  • 77
  • 4
2
votes
1 answer

iOS Swift - How to reference a NSOperationQueue in an extension to cancel existing operations?

I am creating an extension for NSURL that basically validates that a URL exists and returns a valid site. I am using NSURLConnection.sendAsynchronousRequest(...) for this process to actually check the http header response, and it works…
JimmyJammed
  • 9,598
  • 19
  • 79
  • 146
2
votes
1 answer

Disambiguating Initializers across Swift Modules

This question led me to uncover this issue. If I have two initializers with the same signature in the same class in different modules, how can I specify which one I want to call? To use the same example as in that question, say I declare a…
ahruss
  • 2,070
  • 16
  • 21
1
vote
1 answer

Dependency Injection in Protocol/Extension

I am following along with this tutorial in order to create an async generic network layer. I got the network manager working correctly. https://betterprogramming.pub/async-await-generic-network-layer-with-swift-5-5-2bdd51224ea9 As I try to implement…
mikemgg123
  • 69
  • 4
1
vote
1 answer

How to get min value from Integer Enum

I have a enum that has an integer representation, and I am going to be iterating over this type in a for loop. I was loop to get both the min and max int value from the enum private enum PreloadedArrayDataIndex: Int, CaseIterable { case…
mikemgg123
  • 69
  • 4
1
vote
1 answer

How to make hours and minutes of Swift's DateComponents comparable if not this way?

Abstract The requirements of the app I'm working on make me use DateComponents as a bridge when working with time - specifically hours and minutes. Another requirement made me make DateComponents: Comparable and that's when I found some nasty…
1
vote
1 answer

Default Swift protocol implementation in extension for class that conforms to other protocol

I have a class where some subclasses conform to Protocol1. The API from Protocol1 is sufficient to implement Protocol2. I want to do something like below, where I can define a default implementation of Protocol2 when an instance of my class…
1
vote
1 answer

SwiftUI Text Editor with a floating label

I am trying to style a Text Editor in SwiftUI to look like a Material Design element. Currently, I'm struggling with the floating label. I cannot figure out how to capture the editing state of Text Editor. This is the code of the app: struct…
1
vote
3 answers

Create a Swift extension with a where clause that filters on a struct that takes a generic

I'm trying to create an extension on Set that uses a where clause so that it only works on a struct I have that accepts a generic. But I keep running into errors about it the extension wanting the generic to be defined in the struct In this example…
slidmac07
  • 387
  • 1
  • 2
  • 14
1
vote
1 answer

swift5 - enum protocol - Self and self and a variable

I like to separate soms function and var from a enum and thought this was a way. (Just sample code) This results in a compile error "Type 'Self' has no member 'allCases'" public protocol EnumFunctions: Hashable { static var numOfCases: Self {…
iPadawan
  • 898
  • 1
  • 12
  • 23
1
vote
1 answer

How to reuse function in protocol with `associatedtype`?

Consider the following situation: protocol P { associatedtype T = String func f() } extension P { func f() {print("I want to reuse this function")} } class A: P { func f() { (self as P).f() // can't compile …
1
vote
1 answer

RxSwift: Completable concat in a deferred way

Can anybody tell me if it is possible to create a deferred completable in a concat operator. I want to fetch a session, and after this load a user with the corresponding session id. SessionAPI.post(email: email, password: password) UserAPI.get(id:…
PAK
  • 431
  • 4
  • 17
1
vote
0 answers

Is there a way to simply unit test Sirikit intent extension implementations?

I don't seem to find a way to easily add unit tests to Sirikit extension. I would like to be able to test Siri implementation in my app with some unit test to be able to assure myself of non regression with the evolution of the feature but I can't…
Martin
  • 843
  • 8
  • 17