Questions tagged [swift-extensions]

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

Apple Pre-release Docs

394 questions
3
votes
0 answers

Is it possible to write an extension for a specific swift Dictionary ... [String : AnyObject]?

I am trying to write an extension for this: extension [String : AnyObject] { } It throws and error. I was also trying to come up with something similar as this: extension Dictionary: [String : AnyObject] { } It also fails. Is there a solution…
Matej Ukmar
  • 2,157
  • 22
  • 27
3
votes
2 answers

In a Swift extension, get the actual calling object?

Say we are in an instance of SomeClass, consider this simple call NSNotificationCenter.defaultCenter().addObserver( self, selector: #selector(SomeClass.fixer(_:)), name:"FixerNote", object:nil) Say we decide to make an extension to save typing,…
Fattie
  • 27,874
  • 70
  • 431
  • 719
3
votes
1 answer

Swift - extending a class only when it conforms to a protocol to use method swizzling

I have a protocol called NakedNavigationBar. I also have an extension that extends all UIViewControllers that conform to NakedNavigationBar. The problem is that in the extension I want to add default behaviour so that when the UIViewController…
Adam Carter
  • 4,741
  • 5
  • 42
  • 103
3
votes
1 answer

Implement protocol through extension

I'm trying to create a protocol that wraps the process of using the UIImagePickerController to make it more stream-lined in my apps. I essentially have something like this: public protocol MediaAccessor : UIImagePickerControllerDelegate,…
Kevin DiTraglia
  • 25,746
  • 19
  • 92
  • 138
3
votes
6 answers

Convert Integer to Roman Numeral String in Swift

I am looking to take an Integer in Swift and convert it to a Roman Numeral String. Any ideas?
Brian Sachetta
  • 3,319
  • 2
  • 34
  • 45
3
votes
1 answer

Default implementation of protocol method with Swift extension

I'm trying to write default behaviour for a delegate method using a Swift extension as below, but it is never called. Does anyone know why or how to do it the right way? extension NSURLSessionDelegate { public func URLSession(session:…
Stephan
  • 881
  • 9
  • 24
3
votes
1 answer

Swift convenience initializer extension for SKPhysicsBody

extension SKPhysicsBody { /// anchorPoint version of init(rectangleOfSize:center:) convenience init(rectangleOfSize s: CGSize, withAnchorPoint anchorPoint: CGPoint) { var center = CGPoint() center.x = (s.width / 2) - (…
Kent Liau
  • 915
  • 15
  • 26
3
votes
1 answer

How to constrain extension to Dictionary with String Key and array of NSManagedObjects as Value

I would like to add an extension on Dictionary that only applies to a dictionary with Strings as Keys and Array of NSManagedObject as Value Ideally it would look like this: extension Dictionary where Key : StringLiteralConvertible, Value:…
bogen
  • 9,954
  • 9
  • 50
  • 89
3
votes
1 answer

Can I directly access a default static var from the type of a protocol extension?

For Swift fun, I thought I'd build out some alternate reified APIs to GCD. So I threw this in a Playground: import Foundation typealias DispatchQueue = dispatch_queue_t extension DispatchQueue { static var main:DispatchQueue { return…
Travis Griggs
  • 21,522
  • 19
  • 91
  • 167
3
votes
1 answer

In Swift, is it possible for a Objective-C class' properties to satisfy a Swift @obj protocol computed property requirement?

For instance, I want to extract a Swift protocol from my existing Objective-C class MyFoo. Let's call this protocol FooProtocol. The situation looks like this: // In Objective-C @interface MyFoo @property(nonatomic, copy) NSString…
fatuhoku
  • 4,815
  • 3
  • 30
  • 70
3
votes
1 answer

How to use a protocol with optional class methods in an extension with generic in Swift?

I am trying using extension for an existing class with class method like: @objc public protocol MyProtocol { optional class func foo() -> Int } And I am using this protocol in an extension with generic like: extension MyClass { public func…
Wizard
  • 389
  • 1
  • 2
  • 12
3
votes
1 answer

Swift Generics Type Inference Extensions

I am trying to build my understanding of Generics in Swift by creating a Min and Max extension for the Array class (similar to Min and Max Extension methods in C#). There may be a better way to do this, but as I said, it just to help me understand…
fabiossa
  • 136
  • 1
  • 7
3
votes
2 answers

Extending Bool for fun and new control structures in Swift

I'm playing around with Swift extensions, and bumped my head against a strange bug, while trying to extend Bool: typealias Task = ()->() extension Bool{ func untilFalse(task: Task){ while !self {println(self); task()} } } var i =…
cfischer
  • 24,452
  • 37
  • 131
  • 214
3
votes
0 answers

Swift generics and extension functions override

Having a strange problem with generics on Swift have a NSManagedObject subclasses hierarchy with extensions: BaseCloudKitItem<-MyObject extension BaseCloudKitItem { func updateDataWithCKRecord(record: CKRecord!, inManagedObjectContext context:…
iiFreeman
  • 5,165
  • 2
  • 29
  • 42
3
votes
4 answers

swift type constraints on generic extensions

After some amount of stress, I created the following generic function: func removeDupes (inout inputCollection : [T] ) -> [T] { var hashMap = [T : Bool]() var output = 0 for thing in inputCollection { if !hashMap[thing] { …
Gavin
  • 6,495
  • 3
  • 21
  • 22