Questions tagged [swift-extensions]

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

Apple Pre-release Docs

394 questions
4
votes
1 answer

What is the Java equivalent to Swift "extensions"?

According to the official Swift doc (Swift extension doc), the Swift extensions looks like Java enums. Swift extensions: extension Double { var km: Double { return self * 1_000.0 } var m: Double { return self } var cm: Double { return self / 100.0…
kalan nawarathne
  • 1,944
  • 27
  • 26
3
votes
2 answers

Referencing instance method 'xxx' on 'PrimitiveSequence' requires the types 'A' and 'any B' be equivalent

We need some advice. I'm trying to do such abstraction so I have many different Response's. At some time in the project we realized that some of our Responses have id property and we want to make some common logic for those Responses without taking…
Marcin Kapusta
  • 5,076
  • 3
  • 38
  • 55
3
votes
3 answers

Providing default instance arguments for (recursive) extension in Swift

I was having fun while practicing a Kata. I wanted to implement one of the functions in my solution as an extension of Int, because I liked the idea of having pretty syntax. The function, in itself, looks like this: func decomposed(_ n: Int) ->…
3
votes
2 answers

Can protocol be extended with function that would be defined by implementer in Swift?

Example: Playground: github protocol A: class {} extension A { func someFunc() { print("1") } } class B { weak var delegate: A? func someTrigger() { delegate?.someFunc() } } class C: A { lazy var b: B…
えるまる
  • 2,409
  • 3
  • 24
  • 44
3
votes
1 answer

Swift @IBActions in ViewController Extension

I’ve noticed in Swift/Xcode 11 that you can’t ctrl-drag an IB object into a ViewController extension to create an IBAction, but you can cut/paste it into the ext. after ctrl-dragging to create the IBAction in the ViewController class & it works.…
Gallaugher
  • 1,593
  • 16
  • 27
3
votes
1 answer

Why does swift hide default implementation for restricted protocols?

I have a protocol which declaration looks like this: protocol RefreshableView where Self: UIView { func reload() } And it has default implementation which looks as follows: extension RefreshableView { func reload() { print("Default…
The Dreams Wind
  • 8,416
  • 2
  • 19
  • 49
3
votes
4 answers

Optional Protocol methods in swift without using @objc

Using @objc in swift, we can create an optional methods inside protocol like @objc protocol MyProtocol { @objc optional func anOptionalMethod() } But how to create an optional protocol methods without using @objc ?
Ashis Laha
  • 2,176
  • 3
  • 16
  • 17
3
votes
2 answers

What's the difference between an Extension func, Extension static func & Extension class func in Swift?

I was trying to create an extension function on the UIColor which could take a parameter of type Card.Colour and return a UIColor back to the caller. button.backgroundColor = UIColor.getColour(cardColour: cardToDeal.colour) extension UIColor { …
Alex Marchant
  • 570
  • 6
  • 21
3
votes
2 answers

In Swift 4, can you write an extension that applies only to things that adhere to multiple protocols?

Consider these protocols protocol NamedThing{ var name:String{ get } } protocol ValuedThing{ associatedtype ValueType var value:ValueType{ get } } And these structs... struct TestThingA : NamedThing { let name =…
Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286
3
votes
1 answer

Why should not directly extend UIView or UIViewController?

I saw this question, with this code: protocol Flashable {} extension Flashable where Self: UIView { func flash() { UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseIn, animations: { self.alpha = 1.0 //Object…
mfaani
  • 33,269
  • 19
  • 164
  • 293
3
votes
3 answers

How to make an extension for dictionary that allows use of the subscript with a dynamic key?

extension Dictionary where Key: ExpressibleByStringLiteral, Value: Any { func date(forKey key: String) -> Date? { return self[key] as? Date } } let dictionary: [String : Any] = ["mydate" : Date(), "otherkey" :…
bogen
  • 9,954
  • 9
  • 50
  • 89
3
votes
0 answers

How to call ambiguous method of extension on swift 3

//-- Module1 import Foundation public extension String { public func testMethod() { print("something Module1 ---------------------") } } //-- Module2 import Foundation public extension String { public func testMethod() { …
gauravds
  • 2,931
  • 2
  • 28
  • 45
3
votes
1 answer

Protection level issues when accessing a struct within an extension

I am attempting to implement the following library into my project: https://github.com/knutigro/COBezierTableView To use this, the following properties can be given custom values: public extension UIView { public struct BezierPoints { …
3
votes
1 answer

Restrict extensions on a type in Swift?

I have a specific class in Swift that I would like to restrict extensions from being created. I tried adding the final keyword, but it does not restrict extensions: final class MyTest { func testFunc() {} } extension MyTest { func…
TruMan1
  • 33,665
  • 59
  • 184
  • 335
3
votes
2 answers

Why won't my NSColor extension see a CGColor?

This baffles me... I'm trying to create a convenience initializer for NSColor which makes one out of a CGColor, but for some dang reason it just refuses to acknowledge that CGColor exists! I have imported Cocoa, and just for sanity I also imported…
Ky -
  • 30,724
  • 51
  • 192
  • 308