Questions tagged [swift-extensions]

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

Apple Pre-release Docs

394 questions
0
votes
1 answer

fileprivate extension vs normal extension

I know the difference between private and fileprivate. But what is the difference between fileprivate extension UIStoryBoard { } and extension UIStoryBoard { }
Savan Kavaiya
  • 33
  • 1
  • 5
0
votes
2 answers

Is there a way to allow a Swift protocol extension to access something in a class that implements said protocol, but which nothing else can access?

Long-winded question I know. An example is probably simpler: var thing = ThingDoer() thing.doSomething() class ThingDoer: DoThings { private func sortOfPrivateDoSomething () {} } extension DoThings { func doSomething () { …
Joseph Beuys' Mum
  • 2,395
  • 2
  • 24
  • 50
0
votes
2 answers

How to Extend URLRequest

The result of trying to write maintainable, clean code: func fetchNumbersFromServer(completion: @escaping (NumbersResult) -> Void) { let urlString = "https://some-site.com/some-file" var request = URLRequest(url: URL.init(string:…
David
  • 3,285
  • 1
  • 37
  • 54
0
votes
2 answers

Swift protocol default implementation for optional readonly variables

I have the following piece of code, the protocol MyDisplayable has three optional Strings, and I have a default implementation of the protocol via extension. My question is, since I'm sure the extension returns the three strings, is there a way I…
Heuristic
  • 5,087
  • 9
  • 54
  • 94
0
votes
2 answers

How can I override this class function?

I have an extension class, where I'm extending UIButton like below, it's working fine. extension UIButton { class func backButtonTarget(_ target: Any, action: Selector) -> UIBarButtonItem { let backButton = UIButton(frame:…
Abhishek Mitra
  • 3,335
  • 4
  • 25
  • 46
0
votes
1 answer

Extension for Resizing an image within a For Loop

I have written an extension to resize a given image: extension NSImage { func resizeImage(width: CGFloat, _ height: CGFloat) -> NSImage { let img = NSImage(size: CGSize(width:width, height:height)) img.lockFocus() let…
techno
  • 6,100
  • 16
  • 86
  • 192
0
votes
0 answers

Why can't I call function that was defaulted through protocol extension?

I have extended the following protocol with a default parameter. protocol XDelegate { func close(response: Bool, status: Status) } extension XDelegate{ func close(response: Bool, status: Status = .UNDEFINED){} } whenever I call…
mfaani
  • 33,269
  • 19
  • 164
  • 293
0
votes
1 answer

UIView extension with optional properties

Here's my code right now: @objc protocol BaseView { @objc optional var header: UILabel { get set } @objc optional var footer: UILabel { get set } } class ViewWithHeader: UIView, BaseView { @IBOutlet var header: UILabel! } class…
Rawr
  • 2,206
  • 3
  • 25
  • 53
0
votes
0 answers

Use original class variable in an Extension that conforms to a Protocol

I have a situation where I want to be able to use an existing field from a class to satisfy an extension to the class that specifies a protocol. Something like this: class SomeThing { var foo: String } protocol MyProtocol { var foo: String…
Kevin
  • 1,132
  • 2
  • 13
  • 24
0
votes
2 answers

Storing values in array vs creating an extension function in Swift

I was storing couple of UIColors inside an array and changed my code to get them as a return value of a function as an UIColor extension. Array version: var colors : [UIColor] = [UIColor(red:0.35, green:0.40, blue:0.45, alpha:1.00),…
mikro098
  • 2,173
  • 2
  • 32
  • 48
0
votes
1 answer

UIButton Extension For UIBarButtonItem

I'm writing an extension by taking a swift file and did codes in following way: import Foundation import UIKit extension UIButton{ func backButtonTarget(_ target: Any, action: Selector) -> UIBarButtonItem { let backButton =…
Abhishek Mitra
  • 3,335
  • 4
  • 25
  • 46
0
votes
1 answer

Extending NSDecimalNumber to conform to ExpressibleByStringLiteral crashes LLDB RPC server

A playground contains only an extension of NSDecimalNumber that conforms it to ExpressibleByStringLiteral and a variable x that attempts to utilize that extension, and the LLDB RPC server crashes: import Cocoa extension NSDecimalNumber:…
Ky -
  • 30,724
  • 51
  • 192
  • 308
0
votes
1 answer

Swift: Extend Array with generics method with namespace

I'm writing some extension methods for swift Array type. The environment is Xcode8.3(8E162) and Swift3.1 I want to make the final code look like [1, 2, 3].cc.find { $0 < 2 } Here cc is like rx in RxSwift and snp in SnapKit. To achieve that, I…
Cokile Ceoi
  • 1,326
  • 2
  • 15
  • 26
0
votes
0 answers

Associated type in protocol extension

I want to create a protocol and extension which I will use in three different view controllers for API loading. protocol WebAPILoader { // URL of remote resource var url: URL? { get set } // Array supposed to store loaded data. …
cap-slow
  • 71
  • 1
  • 8
0
votes
0 answers

Do Swift extensions take priority over subclasses?

I'm thinking of defining: extension UIViewController { override var prefersStatusBarHidden: Bool { return true } } Does this also work for subclasses, like: class UINavigationController: UIViewController { override var…
Kartick Vaddadi
  • 4,818
  • 6
  • 39
  • 55