Questions tagged [swift-extensions]

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

Apple Pre-release Docs

394 questions
34
votes
2 answers

Can you override between extensions in Swift or not? (Compiler seems confused!)

I've been working on an iOS application in Swift (much of it being moved from Objective-C). I'm using Core Data and trying to use extensions to add functionality to classes auto-generated from my model. One thing I readily did in Objective-C was…
FTLPhysicsGuy
  • 1,035
  • 1
  • 11
  • 23
33
votes
3 answers

Swift Extension: same extension function in two Modules

Say I have a Framework called SwiftKit, which has a UIView's extension class method named someClassMethod and a property named someProperty within it: // SwiftKit public extension UIView { class func someClassMethod() { …
guojiubo
  • 544
  • 1
  • 7
  • 15
26
votes
1 answer

Extension may not contain stored property but why is static allowed

Extension cannot contain stored property, but why then can static stored property be defined within extension? I also didn't find any documentation mentioning that static property is allowed in extension. extension String { static let test =…
Boon
  • 40,656
  • 60
  • 209
  • 315
24
votes
2 answers

Swift extension of a class ONLY when it conforms to a specific protocol

Hi there =) I was just faced with a design problem where I need to (essentially) do the following: I want to inject a bit of code on viewWillAppear: of any UIViewController subclass that conforms to a protocol MyProtocol. Explained in code: protocol…
22
votes
2 answers

Pass in a type to a generic Swift extension, or ideally infer it

Say you have class Fancy:UIView you want to find all sibling Fancy views. No problem... for v:UIView in superview!.subviews { if let f = v as? Fancy { f.hungry = false } } So, try an extension, public…
Fattie
  • 27,874
  • 70
  • 431
  • 719
21
votes
5 answers

Accessing Obj-C properties in Swift extension file

I started writing Swift extensions on my view controllers. So I have three files right now: My header file, ViewController.h: @interface MyViewController : UIViewController @end My Obj-C implementation file, ViewController.m: @interface…
Enrico Susatyo
  • 19,372
  • 18
  • 95
  • 156
21
votes
4 answers

Can I extend Tuples in Swift?

I'd like to write an extension for tuples of (e.g.) two value in Swift. For instance, I'd like to write this swap method: let t = (1, "one") let s = t.swap such that s would be of type (String, Int) with value ("one", 1). (I know I can very easily…
Jean-Philippe Pellet
  • 59,296
  • 21
  • 173
  • 234
20
votes
2 answers

How to properly use class extensions in Swift?

In Swift, I have historically used extensions to extend closed types and provide handy, logic-less functionality, like animations, math extensions etc. However, since extensions are hard dependencies sprinkled all over your code-base, I always think…
Daniel Saidi
  • 6,079
  • 4
  • 27
  • 29
19
votes
2 answers

Generic Extension for Array in Swift

To be very frank, I am totally new to learn Extension creation and usage. I wanted to create a category (Extension in swift 3.0) which can be used throughout an application to perform repeated operations for Array. Sample Link 1 This is what I have…
Jignesh Fadadu
  • 849
  • 1
  • 10
  • 29
19
votes
1 answer

Access Control for Swift Extensions

Swift Programming Language has this to say about access control for extension: You can extend a class, structure, or enumeration in any access context in which the class, structure, or enumeration is available. Any type members added in an…
Boon
  • 40,656
  • 60
  • 209
  • 315
19
votes
6 answers

Swift - How can I override an extension method in a concrete subclass

I have an extension on UIView implementing a protocol protocol SomeProtocol { var property : Int } extension UIView : SomeProtocol { var property : Int { get { return 0 } set { // do nothing …
Avba
  • 14,822
  • 20
  • 92
  • 192
17
votes
3 answers

Name collisions for extension methods from different frameworks

As a test, I created two frameworks. Both frameworks contain this extension: public extension UIDevice { var extraInfo: UIDeviceExtraInfo { return UIDeviceExtraInfo() } } public class UIDeviceExtraInfo { public var prop: String…
Boon
  • 40,656
  • 60
  • 209
  • 315
14
votes
4 answers

Swift map(_:) extension for Set() ?

let numberSet = Set(1...11) let divideSet = numberSet.map({ $0 / 10 }) //Error: Set does not have a member named map :( Swift 1.2 supports Set() for unordered collections, but map(_:) doesn't seem to work on Sets, so i decide to get smart…
Blessing Lopes
  • 570
  • 1
  • 5
  • 11
13
votes
2 answers

Swift 'open' keyword & overridable method/properties in extension?

With introduction of open keyword in Swift 3.0 (What is the 'open' keyword in Swift?). Note: Limited to extensions on NSObject derived classes or @objc attributed method/properties. Code which declared and used public (class) methods/properties in…
Nocross
  • 251
  • 1
  • 4
  • 6
13
votes
2 answers

Self, protocol extension and non-final class

I tried write a static method for UIView which instantiates view of that class from the nib. Method should be generic and work on every UIView subclasses. Also I want to save the type information – so, for example, in this code let myView =…
Alexander Doloz
  • 4,078
  • 1
  • 20
  • 36
1
2
3
26 27