Questions tagged [swift-extensions]

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

Apple Pre-release Docs

394 questions
1
vote
2 answers

Why extension to UIImage is not working?

I'm following the tutorial here to display GIF image on UIImage. I have write the extension UIImage+GIF.swift which basically like this: extension UIImage { public class func gifImage(named: String) -> UIImage { ... } } along with…
Chen Li Yong
  • 5,459
  • 8
  • 58
  • 124
1
vote
1 answer

Swift generic extension for an array in array

I want to define a extension to Array (or Sequence or Collector?) so I can query a list of lists of a custom object with a NSIndexPath and get the object based on the indexPath's section and row. public var tableViewData = [[MyCellData]]() //…
Sunkas
  • 9,542
  • 6
  • 62
  • 102
1
vote
1 answer

How to pass variables from a class to an extension of an object that the class is using?

I have been on this for awhile now and no matter what I look up I can't seem to understand this. Suppose I'm extending UIView to add a toolbar on keyboard, I am not entirely sure how to pass parameters I need to access from the class that is using…
TheBen
  • 3,410
  • 3
  • 26
  • 51
1
vote
1 answer

Extension of Array of FloatingPoint Elements in Swift 3.0

How can I compactly write an extension of an Array in Swift 3.0 which works for both Float and Double Element types? The following doesn't work: extension Array where Element: FloatingPoint { public func multiply(mult: Double) -> [Double] { …
Danra
  • 9,546
  • 5
  • 59
  • 117
1
vote
2 answers

Extending Collection with a recursive property/method that depends on the element type

In the context of this question, I though about how one could implement a property or method that counts across all nesting levels in collections. Intuitively, something this should work: extension Collection { var flatCount: Int { if…
Raphael
  • 9,779
  • 5
  • 63
  • 94
1
vote
1 answer

How to cast Protocol to NSObject to access constrained extension interface?

The question refers to Swift3. Since version 4 you can finally cast objects to Type like this: someObject as (Type & Protocol) I have a protocol and multiple types inheriting from it. A, B, C, D: CKEntity (pseudocode) For that protocol…
Hexfire
  • 5,945
  • 8
  • 32
  • 42
1
vote
1 answer

Access static property of protocol extension

I'm trying to build a protocol exposing a static property, then use that static property in an extension of that protocol, but it seems to work only if I define this static property in the protocol extension as well. Basically the code I'm trying to…
axelcdv
  • 753
  • 6
  • 18
1
vote
1 answer

XCSourceEditorCommandInvocation swift extension causes "unrecognized selector sent to instance" exception

When I try and add swift extensions to classes in XcodeKit (framework for adding Xcode extensions) the compiler is happy to build without any errors, but when the code runs I get the following exception: -[XCSourceEditorCommandInvocation test]:…
ospr
  • 1,650
  • 2
  • 17
  • 21
1
vote
1 answer

Swift Extension of Array with Equatable Elements Cannot Call Index(of:)

I am attempting to add an extension to the Array type in Swift limited to Arrays whose elements conform to the equatable protocol. I am attempting to define a function in the following manner: import Foundation extension Array where…
user3847320
  • 823
  • 9
  • 18
1
vote
2 answers

.sort in protocol extension is not working

I have a protocol as well as a protocol extension and I'd like to implement a function in the protocol extension to sort an array defined by the protocol with custom objects, but it's not working. protocol MyProtocol { var myArray: [MyObject] {…
eLwoodianer
  • 674
  • 6
  • 12
1
vote
0 answers

Cast nsdata to custom protocol value dictionary in swift

I have a custom protocol with which I have extended some structures in Swift. Code example: protocol someprotocol { func foo() -> Int } extension Int : someprotocol { func foo() { return 4 } } I do this with many structs like int8,…
gregx73
  • 23
  • 4
1
vote
1 answer

Subclass an existing Swift Singleton

I am adding functionality to an AWS class that creates a singleton (AWSIdentityManager) and the code for that class is in flux (AWS are improving it). I would like to make my added functionality more distinct from the AWS code so that I don't have…
Bruce0
  • 2,718
  • 1
  • 17
  • 21
1
vote
2 answers

How to share custom swift extensions I built and find really useful

I'd like to share some really useful swift extensions that I built for my apps. I'm sure there are many developers that are looking to easily add the custom functionality to their apps. The only issue is I can't find a good way to share these…
Eugen Dimboiu
  • 2,733
  • 2
  • 26
  • 33
1
vote
1 answer

Can Swift enums be inferred and restricted generically?

I can't find the syntax, but I want to do something like this: class MyClass { let stringValue: String // filled in later let integerValue: Int // filled in later init(stringValue: String) { self.stringValue = stringValue …
Ky -
  • 30,724
  • 51
  • 192
  • 308
1
vote
2 answers

Get values of variables defined by protocol from any VC

protocol(myProtocol):- protocol myProtocol { var type:String { get set } var sub:String { get } var msg:String? { get set } } Class(myVC):- class myVC: UIViewController, myProtocol { //Protocol Declarations var…
Meet
  • 1,196
  • 12
  • 26