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

Can you extend only RawRepresentables which use Strings for the raw value type?

I'm trying to write an extension that extends enums based on strings. The way I know to extend all enumerations is to extend RawRepresentable, but I want it restricted to strings-only. extension RawRepresentable where RawRepresentable.RawValue ==…
Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286
2
votes
1 answer

Extensions may not contain stored properties in UItextfield

extension UITextField @IBInspectable var placeholdercolor: UIColor { willSet { attributedPlaceholder = NSAttributedString(string: placeholder != nil ? placeholder! : "", attributes:[NSAttributedStringKey.foregroundColor:…
Deepti Raghav
  • 852
  • 1
  • 9
  • 26
2
votes
0 answers

How to add protocol conformance to a generic type?

I would like to make CountableClosedRange conform to the ExpressibleByIntegerLiteral protocol so that I can create "single integer ranges" (see my other question). The goal is to be able to assign integer literals to variables of type…
Mischa
  • 15,816
  • 8
  • 59
  • 117
2
votes
1 answer

How to override and inherit the extension in Swift?

I'm new to iOS development and have read articles about the extension. But I couldn't find any examples about extending and inheriting from an extension. Take SpreadsheetView as an example. I want to override the extension SpreadsheetView. I found…
Qooe
  • 673
  • 2
  • 7
  • 13
2
votes
1 answer

Swift - Invoke Custom Delegate Method

I am using BMPlayer library and want to implement custom control, for which I have the following class which confirm to following protocol @objc public protocol BMPlayerControlViewDelegate: class { func controlView(controlView:…
Ibrahim Azhar Armar
  • 25,288
  • 35
  • 131
  • 207
2
votes
0 answers

Extending existing protocol to conform to another protocol

Hello my goal here is to extend (in swift 3) a protocol I have in a framework to conform to another Protocol protocol SomeProtocol {} protocol SomeOtherProtocol {} extension SomeOtherProtocol: SomeProtocol {} The problem here is that I get an…
Massimo
  • 159
  • 1
  • 10
2
votes
2 answers

How can I extend protocol with private set?

I'm writing a protocol that has a readOnly label. I want to extend it and give it a default implementation where the conforming type is a UITextView. Code: protocol CountingView { var keyboardLabel : UILabel {get} } extension CountingView…
mfaani
  • 33,269
  • 19
  • 164
  • 293
2
votes
2 answers

Swift read private property from a .m file

I have a library that is written in Objective-C and using .m and .h files. Is it possible for me to write an extension method for that class and get access to a private property that is only defined in the .m file and not defined in the .h file? I…
Jeggy
  • 1,474
  • 1
  • 19
  • 35
2
votes
1 answer

Swift 3: Add UIButton extension to ViewController

I'm a beginner in iOS/Swift and trying to create a simple application without Storyboard. I created a UIButton extension and I would like to add a simple button to my view (constraints will be set later). Unfortunately the button is not visible. I…
Attila Marosi
  • 144
  • 1
  • 8
2
votes
0 answers

Swift Extension Separate File

I want use a method defined in an extension to UIImageView in my ViewController. When the extension is in the same file as the view controller it works, however - I want to move it to it's own separate/public file, UIImageView+Download.swift. When I…
Brandon Lee
  • 194
  • 2
  • 12
2
votes
1 answer

An extension hides a property that I want to access. Workarounds?

I am using two pods: DropDown and SwiftyUtils. DropDown adds in a UIView subclass called DropDown. The DropDown class defines its own width property. Instead of setting the frame, the client code has to set the width of the drop down menu using this…
Sweeper
  • 213,210
  • 22
  • 193
  • 313
2
votes
1 answer

Extending a struct which is inside another struct

I'm trying to extend a struct that is already inside a struct. When I write the following code, I get declaration is only valid at file scope. struct A { struct AA { } } extension A { extension AA { } } Is it invalid to write…
haider_kazal
  • 3,448
  • 2
  • 20
  • 42
2
votes
1 answer

how to use a static method in objective-c defined in extension

I have a static method created on extension of Date. I am trying to call this method in Objective-C file. I am getting error as "No Known class for selector method name". More detail: In SWIFT 3.0 extension Date { static func …
Raj
  • 67
  • 9
2
votes
1 answer

Swift protocol with generic types

I'm trying to create a protocol that has a static method that returns a generic type. In most cases what I have seems to work reasonably well. The challenge comes in when I want to use an extension to return this generic value. Here's what I have.…
jervine10
  • 3,039
  • 22
  • 35
2
votes
1 answer

ArrayType in extension where clause

extension Array where Element: _ArrayType, Element.Generator.Element: Any { func transpose() -> [Element] { if self.isEmpty { return [Element]() } let count = self[0].count var out = [Element](repeating: Element(),…
Burhanuddin Sunelwala
  • 5,318
  • 3
  • 25
  • 51