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…
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.…
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…
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…
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…
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,…
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…
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…
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 {
…
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…
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…
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 = [
…
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 =…
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…
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 =…