Questions tagged [swift-extensions]

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

Apple Pre-release Docs

394 questions
1
vote
2 answers

Unable to make a protocol for enum

I am not very good with protocols, and I'm quite stuck right now. I am trying to make a protocol for an enum, which has a name property (or function if it needs to be). This will allow me to get the name of an enum value, similar to conforming to…
George
  • 25,988
  • 10
  • 79
  • 133
1
vote
4 answers

Swift Decodable, Endpoint returns completely different types

With API I'm working with, I have a case where 1 API Endpoint can return completely different responses, based on if the call was successful or not. In case of success, API Endpoint returns an Array of requested objects, in the root, something like…
jovanjovanovic
  • 4,158
  • 3
  • 22
  • 32
1
vote
3 answers

Swift: how to stop exposing the delegate methods

WorkAround: I am creating a utility class let's say BLEScanManager, which is responsible to scan nearby BLE devices. The only job of the utility class is to scan BLE devices and make a list of it. The other classes can create an object of this…
Kampai
  • 22,848
  • 21
  • 95
  • 95
1
vote
0 answers

Can I conform some tableview delegate methods in swift extension for objc class

I'm trying to add some UICollectionViewDelegate methods for my objc view controller MyViewController.m; And that view controller already conforms to and has implemented some delegate methods; However I don't want to add…
RobotX
  • 208
  • 2
  • 15
1
vote
1 answer

How to avoid using two extensions with the same method for 2 different classes

I would like to know what is the best way to deal with this situation. I have two different view controllers, both will use the same didUpdateLocations method from CLlocationManagerDelegate. I'm using an extensions for both of them and conform them…
Vangola
  • 128
  • 7
1
vote
1 answer

Checking if two struct instances are different in Swift elegantly

I made a state engine for Swift but there is something that I would like to improve in a significant way. It is an open-source project, you can check it out here: https://github.com/mrtksn/SwiftyState The thing that I would like to improve is the…
mrtksn
  • 412
  • 3
  • 18
1
vote
1 answer

Protocol methods in a class extension are not called under specific conditions

I encountered a weird behavior. The best way I can put it is … Not overridden protocol methods in a class extension are not called while the superclass already conforms to the protocol (via extension). However this happens only while it's build with…
Jakub Truhlář
  • 20,070
  • 9
  • 74
  • 84
1
vote
1 answer

How to resolve collision between protocol and class fields types?

I have a protocol and a class which I want to extend. The protocol requires field of some type and the class has a field with the same name and the type as Implicitly Unwrapped Optional of this type. Can this class be extended by this protocol? If…
1
vote
1 answer

UIView extension in Swift

I have placed a UIView extension in a utilities class written in swift. However, when I try to call a method from the extension, The compiler tells me the The member does not exist Here is my extension: extension UIView { func addShadow() { …
user1904273
  • 4,562
  • 11
  • 45
  • 96
1
vote
0 answers

swift why use an extension

I'm relatively new to coding in general and to swift, but i'm working through an SQLite tutorial (https://www.raywenderlich.com/385-sqlite-with-swift-tutorial-getting-started). I don't understand the rationale for using an extension here. I've…
Mark
  • 35
  • 4
1
vote
0 answers

iOS custom keyboard base?

I need to create a custom keyboard that looks/feels pretty much the same like the system keyboards but is for a language that iOS doesn't have: whenever I have to type using the system keyboard, I'm subject to the autocorrect, which not only gives…
entonio
  • 2,143
  • 1
  • 17
  • 27
1
vote
0 answers

How is adding these UIButton extension functions making my app perform better?

After failing to get a live image filter on a Metal view to perform better, I was curious if my UI had some kind of impact. I have an extension on UIButton that adds a blur and shadow to them. If I comment out the stuff inside the functions, and run…
Chewie The Chorkie
  • 4,896
  • 9
  • 46
  • 90
1
vote
1 answer

Swift: Specializing method implementation with protocol extensions

To provide some context: P stands for property. The purpose of the code is that values of different types should be handled by individual methods (e.g. serializeInt, serializeDouble etc.), something like method overloading but the type of the…
E. Vladov
  • 61
  • 1
  • 3
1
vote
3 answers

Memory Leak in UIButton extension for fadeOut animation in swift4

I built an extension to the UIButton class to do fadeOut. When I use this I get memory leak warning in the profiler. I am using Swift 4 and Xcode 9.3. Thanks in advance for any help. extension UIButton { func fadeOut() { let fadeOut =…
1
vote
1 answer

Why we can give variable instance in Extensions of protocols?

Why can we give an instance to variable in protocol extensions while we can't give an instance to variable in the protocol itself? In the protocol, I can't give a variable new instance but when I make an extension from this protocol I can make a new…