Questions tagged [swift-extensions]

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

Apple Pre-release Docs

394 questions
1
vote
1 answer

Widespread Swift Protocol for Extensions

Widespread Protocol for Extensions Swift 4.1, Xcode 9.3 I was wondering, what are some of the most overarching protocols in Swift. I want to make an extension that applies to values that can be set. The purpose of this was to make it easier to write…
Noah Wilder
  • 1,656
  • 20
  • 38
1
vote
4 answers

Generic `getText()` function for UI elements in Swift

For a lot of actions, I need to check if the content of label.text, textView.text, textField.text etc. is nil. So I created some extensions: extension UILabel { func getText() -> String { return self.text ?? "" } } extension…
kuzdu
  • 7,124
  • 1
  • 51
  • 69
1
vote
0 answers

websites blocking through Content Blocking in Swift

I am creating an app where I want to block few websites by using Content Blocking Extension in Swift. If you guys have any idea to block websites through an app please suggest something.
1
vote
0 answers

Swift 4: type(of: self) differs when using private/fileprivate

I implemented an extension to NSObject to get the dynamic type of my objects: extension NSObject { var dynamic_type : String { get { return String(describing: type(of: self)) } } } This works perfectly for…
user7246017
1
vote
1 answer

Can't extend extension added structs on swift

I have CustomClass like so: class CustomClass { let attribute: Int } In another file I've added it a StructureA via extension like so: extension CustomClass { struct StructureA { let someData: Int } } In a third file, I'm…
Lucas Pereira
  • 569
  • 1
  • 11
  • 23
1
vote
0 answers

NSAttributedString swift extension is not accessable in Objective C

I have created a swift extension for NSAttributedString. import Foundation extension NSAttributedString { public func trimming(_ charSet: NSCharacterSet) -> NSAttributedString { let modifiedString =…
1
vote
1 answer

Error claims a protocol is a generic type, but no generics are used

I am wishing to implement a pattern similar to Notification.Name where anyone can add one later via an extension, like this: Swift 4 public protocol Foo {     var bar: String { get } } public struct FooImpl: Foo {     public let bar:…
Ky -
  • 30,724
  • 51
  • 192
  • 308
1
vote
2 answers

How can I translate this utility function into an extension function?

I wrote this utility function in Swift 4: func insert(_ value: Element, into dictionary: inout [Key : Set], at key: Key) { if let _ = dictionary[key] { dictionary[key]?.insert(value) } else { var…
Ky -
  • 30,724
  • 51
  • 192
  • 308
1
vote
1 answer

Use an extension to extend multiple classes

I am using a tableview datasource extension as shown below. I want to apply this extension to multiple tableview controllers in my app. I can't see any simple way to allow a single generic extension to extend multiple classes without copy & pasting…
lozflan
  • 835
  • 10
  • 23
1
vote
1 answer

Importing only specific extensions from a module

Say I have some extension in module A: public extension String { func foo() { ... } } In module B, I would like to sometimes import individual extensions (or files), just like I can import struct, import enum, ... Neither of import struct…
Raphael
  • 9,779
  • 5
  • 63
  • 94
1
vote
1 answer

Why we use Extension?

As we have Object Oriented Programming, so we can make parent class which have all the functions those are needed for all child classes. so what is the purpose of extensions? I'm little bit confused in that question, please anyone help me.
1
vote
0 answers

Xcode markup/documentation not working

I created an extension and added a description using /// extension Array where Element: UIView { /// Remove each view from its superview. func removeViewsFromSuperview(){ self.forEach{ $0.removeFromSuperview() } } } However,…
14wml
  • 4,048
  • 11
  • 49
  • 97
1
vote
1 answer

Generic IBDesginables UIView extension

I would like to create generic extension for class to add some designables functionality to any UIView subclass and by this avoid adding functionality to all subclasses. So, would be nice to add extension for UIView which conforms to protocol…
Paulius Vindzigelskis
  • 2,121
  • 6
  • 29
  • 41
1
vote
1 answer

In Swift, can you write an extension which returns a typed instance of the class the extension is on?

This is one of those things that seems simple enough, but doesn't work as you'd expect. I'm working on a 'fluent/chaining'-style API for my classes to allow you to set properties via functions which can be chained together so you don't have to go…
Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286
1
vote
1 answer

Derive type for extension's static func

I want to make a method to return all NSManagedObject of a specific class as an extension: extension NSManagedObject { static func getAll() -> [NSManagedObject]? { // code } } How can I specify the exact type of returned objects? So,…