Questions tagged [swift-extensions]

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

Apple Pre-release Docs

394 questions
12
votes
1 answer

What is extension_access_modifier swiftlint?

I added Swiftlint to a project and I'm having trouble understanding what the warning is for extension_access_modifier. I see it mainly on a class that is declared as public, but there are extensions littered throughout the codebase that adds…
Crystal
  • 28,460
  • 62
  • 219
  • 393
12
votes
2 answers

Add constraints to generic parameters in extension

I have this function: func flatten(dict: Dictionary>) -> Dictionary { var result = [Key: Value]() for (key, value) in dict { guard let value = value else { continue } …
redent84
  • 18,901
  • 4
  • 62
  • 85
11
votes
2 answers

Why 'there cannot be more than one conformance, even with different conditional bounds'?

I hoped that Swift gives me the ability to create an extension for type with specified conditions in where block. I imagined that I can extend the same generic type with different extensions dependent on concrete generic type value (T). But not.…
Valentyn Zakharenko
  • 2,710
  • 1
  • 21
  • 47
11
votes
3 answers

Handling class initialization failure in Swift extension

I'm rewriting an Objective C category below to Swift: @implementation UIImage (Extra) + (UIImage *)validImageNamed:(NSString *)name { UIImage *image = [self imageNamed:name]; NSAssert(image, @"Unable to find image named '%@'", name); …
Zmey
  • 2,304
  • 1
  • 24
  • 40
10
votes
2 answers

Swift extension - Constrained extension must be declared on the unspecialized generic type 'Array'

I have an API that returns a JSON array of objects. I've setup the structure to look as follows: typealias MyModels = [MyModel] struct MyModel: Codable { let field1: String let field2: String let mySubModel: SubModel? enum…
svguerin3
  • 2,433
  • 3
  • 29
  • 53
10
votes
1 answer

Non-'@objc' method does not satisfy optional requirement of '@objc' protocol with conditional extension

I'm trying to create a default implementation of an MKMapViewDelegate by using the conditional extension as follows: extension MKMapViewDelegate where Self: NSObject { func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) ->…
10
votes
3 answers

Subscript Dictionary with String-based Enums in Swift

I want to extend Dictionary with String keys (JSON dictionaries) to allow subscripting with any enum that has a RawValue type of String. The end goal would be multiple enums that can be used to subscript JSON dictionaries. enum JSONKey: String { …
keithbhunter
  • 12,258
  • 4
  • 33
  • 58
10
votes
3 answers

Swift extension for selected class instance

In Objective-C category, you can bring in the extended capability introduced by the category methods by including the header of the category in your class. It seems like all Swift extensions are automatically introduced without import. How do you…
Boon
  • 40,656
  • 60
  • 209
  • 315
10
votes
2 answers

Self.Type cannot be directly converted to AnyClass in extension to objective-c class in swift

I'm trying to create fabric method to create UIViewController with correct nib name (to fix iOS8 default initialiser issue). To do it I have added extension: extension UIViewController { class func create() -> Self { if…
reqzix
  • 503
  • 1
  • 6
  • 12
10
votes
3 answers

Accessing Swift Extension From Objective-C

I'm having trouble accessing my swift extension from objective-c. I have the following code in a .swift file: extension NSDictionary { func dictionaryAsJsonString() -> NSString { var err: NSError? var data =…
Olshansky
  • 5,904
  • 8
  • 32
  • 47
9
votes
1 answer

Swift 3.0: compiler error when calling global func min(T,T) in Array or Dictionary extension

After converting from Swift 2.2 to 3.0 my Array extension does not compile anymore, because it contains a call to global standard library function min(T,T) and shows compiler error extra argument in call. Here's a simple way to reproduce the…
Goodsquirrel
  • 1,476
  • 1
  • 15
  • 29
8
votes
2 answers

'let' property may not be initialized directly; use "self.init(...)" or "self = ..." instead

I want to try to write a default init in a protocol extension, so I did this: protocol P { var data: [AnyHashable: Any] { get } init(_ s: String) } extension P { init(_ s: String) { self.data = ["s": s] } } But I'm getting…
8
votes
1 answer

Implementing a function with a default parameter defined in a protocol

Swift protocols can provide default implementations for functions and computed properties by adding extensions to them. I've done that plenty of times. It is my understanding that the default implementation is only used as a "fallback": It's…
Mischa
  • 15,816
  • 8
  • 59
  • 117
8
votes
1 answer

Cannot declare a public protocol extension with internal requirements

I am programming a media player app and created my own framework for managing all the player functionality. In this framework I have a public protocol called PlayerControllerType and an internal protocol _PlayerControllerType. In…
Kai Engelhardt
  • 1,272
  • 13
  • 17
7
votes
2 answers

Optional extension for any types

I want to write Optional extension for any types. My code for integer: extension Optional where Wrapped == Int { func ifNil(default: T) -> T { if self != nil { return self as! T } return default …
emrcftci
  • 3,355
  • 3
  • 21
  • 35
1 2
3
26 27