Questions tagged [swift-extensions]

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

Apple Pre-release Docs

394 questions
5
votes
0 answers

Creating an SKAction extension for SKAudioNode

I have decided to break off my very painful relationship with SKAction.playSoundFileNamed and move on to using SKAudioNodes in our project -- the breaking point was their being totally broken by interruptions without any consistency. We are trying…
5
votes
2 answers

Swift: Extension on [?] to produce [?] possible?

In Swift, I have a custom struct with this basic premise: A wrapper struct that can contain any type that conforms to BinaryInteger such as Int, UInt8, Int16, etc. protocol SomeTypeProtocol { associatedtype NumberType var value: NumberType…
stef
  • 952
  • 10
  • 15
5
votes
5 answers

Dictionary Extension that Swaps Keys & Values - Swift 4.1

Dictionary Extension - Swap Dictionary Keys & Values Swift 4.1, Xcode 9.3 I want to make a function that would take Dictionary and return said dictionary, but with its values as the keys and its keys as its respective values. So far, I have made a…
Noah Wilder
  • 1,656
  • 20
  • 38
5
votes
1 answer

Extension of protocol with method which adds default parameter

I'm used to having default parameters inside protocols using extensions, as protocol declarations themselves can't have them, like this: protocol Controller { func fetch(forPredicate predicate: NSPredicate?) } extension Controller { func…
Vladyslav Zavalykhatko
  • 15,202
  • 8
  • 65
  • 100
5
votes
1 answer

Alternative to override extension's method

I want to extend UIView by adding some functions, and override them in any subclass of UIView that I want. I found in apple documentations that I can't override extensions (and the compiler will complain) which make some sense. So I need someone to…
Ahmed Fathi
  • 218
  • 1
  • 11
5
votes
1 answer

How to use @objc protocol with optional and extensions at the same time?

This code does not compile and might sound stupid as it is, but i'll explain why it's so important! @objc protocol p { optional func f1() func f2() } extension p { func f1() { } func f2() { } } class foo: p { } Compiler says Type…
farzadshbfn
  • 2,710
  • 1
  • 18
  • 38
5
votes
1 answer

Default implementation of protocol implementing protocol

I'm trying to create a protocol that would be implemented by certain classes, all of them should also implement UIScrollViewDelegate. What I thought of is for my new protocol to implement the protocol UIScrollViewDelegate. protocol MyProtocol:…
5
votes
6 answers

keyPathsForValuesAffecting with NSManagedObject

Bonjour, I would like to translate an objective'c exercise from an Aaron's book to swift but I can't find the solution. The Objective'c code is : @dynamic firstName; @dynamic lastName; @dynamic department; + (NSSet…
Jan Leenneje
  • 101
  • 1
  • 7
5
votes
1 answer

Swift Extension fails adding overloaded methods to Objective-C class

If a Swift extension is used to add overloaded methods to an Objective-C class, it appears to only call the first method, producing unexpected behaviour or a crash at runtime. An Extension on a Swift class (as opposed to Objective-C) works…
brunobowden
  • 1,492
  • 19
  • 37
5
votes
1 answer

Calling mutating func on an Int extension

Created an extension in a command line app. When I tried calling the calc method, it won't work. Calling desc works. What did I miss? protocol Calculatable { var desc:String { get } mutating func calc() } class MyClass : Calculatable { …
Boon
  • 40,656
  • 60
  • 209
  • 315
4
votes
1 answer

What is the scope of extensions in swift?

I'm learning the swift programming language and recently came across the concept of extensions, where you can extend the functionality of an existing class or struct. For example (source): extension String { func trimmed() -> String { …
Cam
  • 14,930
  • 16
  • 77
  • 128
4
votes
1 answer

Dynamic dispatching protocol extension doesn't work multiple targets

This is my code in my main target(so not the test target): protocol ProtocolA { func dontCrash() } extension ProtocolA { func dontCrash() { fatalError() } func tryCrash() { dontCrash() } } class MyClass:…
J. Doe
  • 12,159
  • 9
  • 60
  • 114
4
votes
3 answers

In Swift, does a protocol extension allow function bodies?

I'm going through a tutorial and I noticed that the author extended their protocol called Activity and wrote the function's body in their code. This does compile however I was under the impression that protocols only show method signatures or if it…
Laurence Wingo
  • 3,912
  • 7
  • 33
  • 61
4
votes
1 answer

Updating object properties with codable extension in Swift

let me start by saying that I have already implemented Decodable which decodes JSON into several objects with these two Integer values: public class ARBufferData: DecoderUpdatable { private var previousStation: Int private var…
Sam
  • 51
  • 1
  • 7
4
votes
1 answer

Swift 4.1.2 Autocomplete: Suggesting extensions on Array that it shouldn't

Maybe I'm going crazy here, but extension Array where Element == String in Swift 4.1.2 is exposing its properties in Swift's autocomplete on types when it should not be there. For example, this extension: extension Array where Element == String { …
stef
  • 952
  • 10
  • 15