Questions tagged [swift-extensions]

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

Apple Pre-release Docs

394 questions
2
votes
0 answers

iOS Custom Keyboard Extension Full Context in Field

Currently, I am working on a custom Swift keyboard and using the Keyboard Kit framework. I have researched various solutions, and this one is the best But after I implemented this code for Swift 5, it works when I placed cursor end of the…
excE
  • 70
  • 9
2
votes
1 answer

How to make a proper reactive extension on Eureka SelectableSection

This is my first question to the StackOverflow community so excuse me if I'm doing something wrong. 1. What I'm trying to achieve Basically, I want to make a custom reactive wrapper around Eureka's SelectableSection class in order to observe the…
Radu
  • 23
  • 4
2
votes
1 answer

Swift extension of generic type that is Optional

I would like to extend SwiftUI Binding when its value is any optional type. Is it possible extension Binding where Value == Optional { func filter(_ predicate: @escaping (Value) -> Bool) -> Binding { Binding( …
Michał Ziobro
  • 10,759
  • 11
  • 88
  • 143
2
votes
3 answers

Swift protocol properties with delegates

I am trying to setup a success/error view on a controller via protocol and extensions. What I want to achieve is that I want to get into the state where it is enough to implement the protocol on a controller, and from there get access to the…
rimes
  • 761
  • 1
  • 8
  • 25
2
votes
1 answer

How do I add a toolbar to a keyboard extension?

I'm building a custom keyboard and I can't find any tutorial or article about that. What I'm trying to do is adding a toolbar like the one you can see in the screenshot below. As a reference you can even take the suggested words toolbar in the…
crost
  • 165
  • 1
  • 13
2
votes
1 answer

Call String extension including function written in Swift from Objective-C

When an extension in Swift just uses a variable as follows: extension NSString { var isNumber: Bool { return length > 0 && rangeOfCharacter(from: CharacterSet.decimalDigits.inverted).location == NSNotFound } } I…
user6631314
  • 1,751
  • 1
  • 13
  • 44
2
votes
1 answer

Shared file between app extension and main app

I need your help to figure out how to make my .json file accessible by both the main app and the app extension. I have an extension that, when the post button is pressed, writes some data in a .json file. But when I try to read the file in the main…
user8605192
2
votes
1 answer

Creating a swift extension function for all controllers to access

thanks for taking the time to read my question. i have an app project using swift that has a function using sdwebimage. This function is pretty simple and works perfectly in my code. However, this function is called many times throughout my app and…
DrDev
  • 23
  • 4
2
votes
1 answer

Using extension to create outlets act weird in UITableViewCell

here is my cell class when I'm using extension to create the outlet. and setting the value using didSet method by checking the row index. when I'm scrolling the values change in weird way. import UIKit class MyLabelCell: BaseTableViewCell { …
Marlon Brando aka Ben
  • 863
  • 1
  • 14
  • 33
2
votes
2 answers

How to initialize ui elements in my ViewController extension in ios, swift

I have a view controller which has a programmatically created label like below. class MyController: UIViewController { override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = .white setupUI() } …
Marlon Brando aka Ben
  • 863
  • 1
  • 14
  • 33
2
votes
4 answers

Creating a clickable UIImageView using an extension

I have the following Swift code. extension UIImageView { func enableClickablePrint() { let imageTap = UITapGestureRecognizer(target: self, action: #selector(imageTapped)) self.addGestureRecognizer(imageTap) …
Charlie Fish
  • 18,491
  • 19
  • 86
  • 179
2
votes
1 answer

In Swift, why does the error "type alias references itself" depend on where the type alias is defined?

I'm using Swift 4.1 in Xcode 9.4.1. I need to create a dictionary that can hold a finite variety of data types as values, so I wanted to define the dictionary as something more specific than [String: Any]. I decided to create a protocol, and take…
ConfusedByCode
  • 1,137
  • 8
  • 27
2
votes
1 answer

What's the difference between Element == StringProtocol and Element: StringProtocol?

I keep seeing different syntax for how to extend an array. Here are the two that I'm seeing. Can someone explain what the difference is? extension Array where Element == StringProtocol { func foo(){} } extension Array where…
Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286
2
votes
1 answer

How do you write an extension to support both [String] and [Substring]?

Normally, to write an extension against a [String], you do this... extension Array where Element == String { .... } However, sometimes I don't have a [String] but rather a [Substring]. How can you write an extension that supports either?
Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286
2
votes
0 answers

Why does an explicit static call on a concrete class that returns 'Self?' fail casting, but an implicit static call to that same method works fine?

Given the following class... final class MyClass : Codable { var someDate:Date } And this extension on it... extension Decodable{ static func decodeFromJson(_ json:Data?) -> Self?{ return nil // Implementation not needed for…
Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286