Questions tagged [swift-extensions]

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

Apple Pre-release Docs

394 questions
427
votes
19 answers

How does one declare optional methods in a Swift protocol?

Is it possible in Swift? If not then is there a workaround to do it?
Selvin
  • 12,333
  • 17
  • 59
  • 80
159
votes
6 answers

Overriding methods in Swift extensions

I tend to only put the necessities (stored properties, initializers) into my class definitions and move everything else into their own extension, kind of like an extension per logical block that I would group with // MARK: as well. For a UIView…
Christian Schnorr
  • 10,768
  • 8
  • 48
  • 83
118
votes
6 answers

Can Objective-C code call Swift class extensions?

I searched some posts, I think I cannot write an extension under Swift, and call it from Objective-C code, right? @objc like attributes only support methods, class, protocols ?
sprhawk
  • 1,477
  • 2
  • 10
  • 9
93
votes
9 answers

How to add an optional string extension?

You can create a String extension like so: extension String { func someFunc() -> Bool { return true } } but what if you want it to apply to optional string? var optionalString: String? = "" optionalString!.someFunc() /* String? does not have a…
vol7ron
  • 40,809
  • 21
  • 119
  • 172
88
votes
4 answers

How to create swift class for category?

I want to create category of my existing swift class, but there is no option in IDE to do so. Any idea if category exists in swift project? Or how to achieve similar functionality in swift project?
Khawar
  • 9,151
  • 9
  • 46
  • 67
77
votes
8 answers

Extend array types using where clause in Swift

I'd like to use the Accelerate framework to extend [Float] and [Double] but each of these requires a different implementation. I tried the obvious: extension Array { } and get this error: "Constrained extension must be declared on the…
GScrivs
  • 865
  • 1
  • 7
  • 19
70
votes
4 answers

How to define initializers in a protocol extension?

protocol Car { var wheels : Int { get set} init(wheels: Int) } extension Car { init(wheels: Int) { self.wheels = wheels } } on self.wheels = wheels i get the error Error: variable 'self' passed by reference before being…
bogen
  • 9,954
  • 9
  • 50
  • 89
47
votes
4 answers

Set (Collection) - Insert multiple elements

Set is an unordered collection of unique elements. Almost similar to array. I want to add/insert multiple elements in a Set of String. But there is only single method provided that can insert only one element (accepts single Set element as a…
Krunal
  • 77,632
  • 48
  • 245
  • 261
47
votes
1 answer

Swift 2 Error using mutating function in Protocol extension "Cannot use mutating member on immutable value: 'self' is immutable

Not sure what's going on here, this seems like it should be pretty straight forward. I have a protocol that mutable var, an extension with a mutating function. Things are crapping out in the testClass.testFunc, when I try and use mtkAnimQueAppend…
47
votes
7 answers

Creating an extension to filter nils from an Array in Swift

I'm trying to write an extension to Array which will allow an array of optional T's to be transformed into an array of non-optional T's. e.g. this could be written as a free function like this: func removeAllNils(array: [T?]) -> [T] { return…
Javawag
  • 1,547
  • 2
  • 16
  • 23
45
votes
3 answers

what is 'where self' in protocol extension

I saw so many examples with below format extension Protocolname where Self: UIViewController What is where Self in protocol extension. I couldn't find the documentation on this.
Mini2008
  • 627
  • 1
  • 6
  • 12
42
votes
2 answers

How to make extension for multiple classes Swift

I have an extension: extension UILabel { func animateHidden(flag: Bool) { self.hidden = flag } } I need to make the same one for UIImageView but I don't want to copy that whole code. Is it possible to make an extension for multiple…
Danny
  • 3,975
  • 4
  • 22
  • 35
41
votes
1 answer

Swift "where" Array Extensions

As of Swift 2.0 it seems we can get closer to extensions of generic types applicable to predicated situations. Although we still can't do this: protocol Idable { var id : String { get } } extension Array where T : Idable { ... } ...we can…
yo.ian.g
  • 1,354
  • 2
  • 14
  • 21
37
votes
4 answers

Return instancetype in Swift

I'm trying to make this extension: extension UIViewController { class func initialize(storyboardName: String, storyboardId: String) -> Self { let storyboad = UIStoryboard(name: storyboardName, bundle: nil) let controller =…
ChikabuZ
  • 10,031
  • 5
  • 63
  • 86
36
votes
3 answers

Why can't extensions with protocol conformances have a specific access level?

Assume we have the following example code: protocol MyProtocol { func someFunction() } public class MyClass { } public extension MyClass: MyProtocol { func someFunction() { print("hello") } } Compiling the code above gives…
Paolo
  • 3,825
  • 4
  • 25
  • 41
1
2 3
26 27