Questions tagged [swift-extensions]

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

Apple Pre-release Docs

394 questions
0
votes
0 answers

Using @objc to access optional variable in Swift extension from Objective-C

I am trying to access a Swift Extension to NSString from an objective-C class using the @objc prefix but can't access an optional. Now that Swift 4.2 requires @objc before variables or functions called from Objective-C, to access the extension, I…
zztop
  • 701
  • 1
  • 7
  • 20
0
votes
2 answers

Why would we use extensions?

I've just learned about extensions and I was wondering, there was an example about extending a protocol. For example, let's say we have the protocol: protocol CanFly { func canFly() } which allows all the classes who can fly to basiclly to fly.…
user9212150
0
votes
1 answer

Convert NSArray to Array in extension in Swift

I am trying to make a Swift extension for NSArray that uses some Swift methods but am having trouble with the firsts step of creating a properly formatted Swift Array from the NSArray. The incoming NSArray looks like…
user6631314
  • 1,751
  • 1
  • 13
  • 44
0
votes
1 answer

Swift extension of Sequence to check if an intersection exists

My goal is to have a function available through all Sequence like Array or Set. This function should return a Bool telling whether there is at least one object present in both sequences or not. // Usage let s1 = ["hi", "hey", "ho"] let s2:Set =…
itMaxence
  • 1,230
  • 16
  • 28
0
votes
2 answers

Swift 5 Error: 'UIImage' is ambiguous for type lookup in this context

When I try to create a UIImage extension in Swift 5, like this: extension UIImage { } I get this error: 'UIImage' is ambiguous for type lookup in this context What causes this and how do I avert it? Thanks!
spaine
  • 443
  • 1
  • 5
  • 17
0
votes
2 answers

URL Encoding swift IOS

I am using following code to encode my string url let originalString = "test/test" let escapedString = originalString.addingPercentEncoding(withAllowedCharacters: . urlQueryAllowed) print(escapedString!) I am looking for Swift extension to encode…
amodkanthe
  • 4,345
  • 6
  • 36
  • 77
0
votes
1 answer

Swift UIButton Extension Animations Override

So I have a UIButton Extension that allows all the buttons in my app to have a nice subtle animation. But recently I have thought of giving a few buttons a special animation but I am not sure how to give those few buttons a specific different…
ThDev
  • 13
  • 6
0
votes
1 answer

Ambiguous use of subscript in Xcode 10.2

I have a Swift project in which I use 2 extensions on any Collection type. They allow for safe access to elements in an array. Here's the extensions: subscript (safe index: Index) -> Element? { return indices.contains(index) ? self[index] :…
invalidArgument
  • 2,289
  • 4
  • 24
  • 35
0
votes
1 answer

Is it possible to write a Swift Extension for UIDatePicker.Mode

From the Apple documentation, UIDatePicker.Mode is an enum of type Int with four cases. enum Mode : Int case time case date case dateAndTime case countDownTimer I am curious to know whether I can add a fifth case monthYear (= 4) to enable me to…
andrewbuilder
  • 3,629
  • 2
  • 24
  • 46
0
votes
2 answers

Protection level of an extension

I have a DataManager class and it has some relevant functions and variables. For example, Class DataManager: NSObject { func doSomething() func doSomethingAgain() } I move few method to an extension of DataManager. I made the extension as…
Tapas Pal
  • 7,073
  • 8
  • 39
  • 86
0
votes
1 answer

How to make a class in Swift extend a concrete class and conform to multiple protocols?

Background I am creating an extension for UITableViewController like so: func attachToController(_ sender: C, alignTo: UIView, to: ALEdge = .top , withOffset: CGFloat = 0.0) where C: UITableViewDataSource,…
abbood
  • 23,101
  • 16
  • 132
  • 246
0
votes
2 answers

Swift Extension with delegation (UIViewController)

I need ability to send email in a number of view controllers in my app. The code is same, take three params -- recipient address, body, and subject. If Mail is configured on device, initialize MFMailComposeViewController with the view controller as…
Deepak Sharma
  • 5,577
  • 7
  • 55
  • 131
0
votes
1 answer

How can I make a generic Sequence Filter wrapper? Filter contains same block for both Set and Array types

I have places where I have both Set and Array of MyType. In these places, I need to filter down my Sequences, and you'll notice that the filter block is the same for both Sequence types. Is there any way I can implement a generic Sequence extension,…
A O
  • 5,516
  • 3
  • 33
  • 68
0
votes
2 answers

How can you create an infix operator that applies to every class and struct?

I've created a 'configure' infix operator '=>' which lets me configure objects inline, allowing me to both define and initialize properties and/or call methods/functions at the assignment level. For instance, instead of this, which requires an…
Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286
0
votes
1 answer

Changing colour properties and images for navigation bar, tableview, collection view in Extension Swift 4.2

I am creating night mode using Swift 4.2(Notification Center ). I followed https://medium.com/@mczachurski/ios-dark-theme-9a12724c112d this tutorial and able change the colour for only labels. check the below list what all things need to change…
OhStack
  • 106
  • 1
  • 1
  • 15