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

Swift 3 Generic Extension Arguments

In Swift 2.x, I had a nice little setup that allowed me to store and retrieve dictionary values using enum members: public enum UserDefaultsKey : String { case mainWindowFrame case selectedTabIndex case recentSearches } extension…
Ben Stock
  • 1,986
  • 1
  • 22
  • 30
1
vote
0 answers

Set Global UITextField Delegate

I'm trying to find a way to globally set the delegate of UITextFields used in my app without having to manually do it for each and every UITextField. My initial thought was to do this using a Swift extension, but that doesn't seem to be possible.…
KMLong
  • 8,345
  • 2
  • 16
  • 19
1
vote
1 answer

How to change self in NSDate extension Swift?

I am trying to write an extension where NSDate can be changed to the another date. I want to make an extension which will modify self. I've check different methods but cannot find how to modify self I am trying something like that but it doesn't…
Danny
  • 3,975
  • 4
  • 22
  • 35
1
vote
1 answer

How to extend type that accepts generic elements of array?

I have a type that I'm trying to specifically extend: AnyObserver<[MyModel]>. It would be easy to extend if I wasn't passing in an an array as the Element, which I can do something like this: extension AnyObserver where Element: MyModel…
TruMan1
  • 33,665
  • 59
  • 184
  • 335
1
vote
1 answer

Understanding Swift Index, Range, Distance

I'm implementing an extension to Swift's CollectionType that provides the ability to find a subsequence in the collection and to find the range of that subsequence. My code that's working in a playground is this: extension CollectionType where…
Feldur
  • 1,121
  • 9
  • 23
1
vote
1 answer

Swift Extension exception Execution was interrupted, reason: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

I have tried to do an extension to NSDate. What I wanted is a flag indicating if the NSDate has to be removed later So I have tried this in playground //: Playground - noun: a place where people can play import UIKit var str = "Hello,…
John B.
  • 682
  • 1
  • 11
  • 22
1
vote
1 answer

Access Computed Property of a Protocol in a Protocol Extension

Assume I have a protocol like protocol TypedString : CustomStringConvertible, Equatable, Hashable { } func == (lhs : T, rhs : S) -> Bool { return lhs.description == rhs.description } I want to be able to…
Feenaboccles
  • 412
  • 3
  • 12
1
vote
2 answers

Public Extensions in Swift : Float vs Float.type

I'm trying to build an extension for floats in Swift that returns an enum (case .Positive for positive, .Negative for negative). I built an enum first. public enum Sign { case Positive,Negative } Now, for the extension, public extension…
1
vote
1 answer

swift- Cannot convert value of type '[Int]' to expected argument type '[_]'

I'm trying to implement a zip function as an extension to Array, and I'm intending for it to be used like this: let myKeys = ["a","b","c"] let myVars = [1,2,3] myKeys.zip(myVars) // ["a":1,"b":2,"c":3] Here is my attempt: extension Array { …
OhNo
  • 1,720
  • 1
  • 11
  • 7
1
vote
1 answer

Convert Array extension to SequenceType?

I'm putting together some common functions I use in my app and came up with the below extensions: public extension CollectionType { public func toArray() -> [Self.Generator.Element] { return self.map { $0 } } } public extension…
TruMan1
  • 33,665
  • 59
  • 184
  • 335
1
vote
0 answers

How to extend a dictionary of Protocol values keyed by string?

I have a dictionary, [String: ComponentObject]() in Swift 2 and I have been unable to write an extension for this type. However, when I make ComponentObject a class instead of a protocol, then it works as expected. Here is a playground that…
Wayne Bloss
  • 5,370
  • 7
  • 50
  • 81
1
vote
1 answer

Cannot append in Array extension

I have extended the Array class in order to create some methods. However I am not being able to append to the array: private extension Array { mutating func addCharacterWithIndex(char: String, index: Int) { let dict = [ …
ZeMoon
  • 20,054
  • 5
  • 57
  • 98
1
vote
0 answers

Adding a Subscript to a constrained Array

Given a struct Parameter which has a name and a value property, I would like to add a Subscript to an Array which let me return the value of the first found element whose name matches the subscript parameter. Usage example: let params =…
CouchDeveloper
  • 18,174
  • 3
  • 45
  • 67
1
vote
0 answers

Avoiding public method decorators in a Swift public class extension

Suppose I defined a protocol in Swift: public protocol MyProtocol { func myMethod() -> String } I then define a class in the same source file: public class MyClass: NSObject { var myVariable = "" func myOtherMethod() } I then add an…
1
vote
1 answer

Cannot convert return expression of type 'UIColor' to return type '[UIColor]' on UIColor extension

I'm writing a simple extension for UIColor to take a hex string based off this answer: import UIKit extension UIColor { public static func colorWithString (hex:String) -> UIColor { var cString:String =…
brandonscript
  • 68,675
  • 32
  • 163
  • 220