Questions tagged [swift-extensions]

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

Apple Pre-release Docs

394 questions
0
votes
1 answer

Generic Extension to Array Not Working

I've been playing around with Generics and Extensions to existing types in Swift 3. I wrote two generic Array functions that extends Array with find-and-replace methods, named replaced() and replace(). The replaced() function works as intended but…
RMH
  • 801
  • 2
  • 8
  • 17
0
votes
1 answer

UIImage Resize Extension

I made an extension that lets me resize a UIImage but I'm wondering if I'm calling it correctly. The extension is in it's own file and is like this: extension UIImage { func resizeImage(image: UIImage, targetSize: CGSize) -> UIImage { …
MarksCode
  • 8,074
  • 15
  • 64
  • 133
0
votes
0 answers

Having trouble setting button target in extension of keyboard

I have looked at many examples of extensions, thought can't find this. I am simply trying to use extension to add a toolbar with buttons on top of keyboard in swift, but since extensions can't have stored values easily , is there a simple way to…
TheBen
  • 3,410
  • 3
  • 26
  • 51
0
votes
2 answers

Use of unresolved identifier in UITableViewDelegate extension

Instead of assigning the view controller as the UITableViewDelegate, I'm trying to reduce the code in the view controller by creating an extension for the UITableViewDelegate. Why am I getting the error "Use of unresolved identifier…
14wml
  • 4,048
  • 11
  • 49
  • 97
0
votes
1 answer

How can I extend dictionaries whose values are dictionaries themselves?

Say I want to extend nested dictionaries with some functionality. Using pseudo-Swift, this is my goal: extension Dictionary where Value: Dictionary { typealias K1 = Key typealias K2 = Value.Key typealias V = Value.Value …
Raphael
  • 9,779
  • 5
  • 63
  • 94
0
votes
1 answer

Can one extend a Swift Dictionary by a struct?

I've tried the solution in extension of Dictionary where but it won't compile for me. I simply want to constrain a dictionary extension to struct types. Is there any way of accomplishing this? import Cocoa struct Foo: Hashable…
Ky -
  • 30,724
  • 51
  • 192
  • 308
0
votes
2 answers

How can I add protocol to an element from interfacebuilder in Swift?

If I have an instance of UILabel in storyboard and I have it connected to view controller as IBOutlet. If I want add some functionality only to this label, how can I do it? I know that for obj-c you make a subclass of UILabel. But I wonder if there…
Klemen
  • 2,144
  • 2
  • 23
  • 31
0
votes
1 answer

Can't assign the item in _ArrayProtocol

Hi I'm trying to retrieve data by using ParseLiveQuery My problem is...I can't assign the item in Array.. import Foundation import Parse import ParseLiveQuery extension _ArrayProtocol where Iterator.Element == PFObject { mutating func…
Shawn Baek
  • 1,928
  • 3
  • 20
  • 34
0
votes
1 answer

Change Time Conversion Function to Swift Extension

I have a function that converts minutes to either a decimal or a HH:MM string based on a user preference in NSUserDefaults. For example, 90 minutes would be either 1.5 or 1:30. Here's my function: func decimalOrHHMM(value:Int) -> String{ let…
Clifton Labrum
  • 13,053
  • 9
  • 65
  • 128
0
votes
1 answer

Declaration is on valid at file scope (UI color extension)

Okay so I have an error with my extension, which whenever I add a line or space it up it creates more errors with the UIColor. I can use the Clean function to get it back down to these two errors. My extension is not in {} and I think I have all my…
Darin Wilson
  • 169
  • 1
  • 2
  • 9
0
votes
0 answers

Swift 2 to 3 Migration for NSRange

I'm trying to use a random number extension I created that worked in swift 2 and fails in Swift 3. Not sure why. Here is the code. let aRandomInt = Int.random(range:0...2) //Extention to INT to create random number in range. extension Int { …
Joseph Astrahan
  • 8,659
  • 12
  • 83
  • 154
0
votes
1 answer

How to instantiate a Type in a Type extension method?

I am trying to create a Type extension for UIColor per the code snippet below, but I'm receiving a build error. When I try to create the UIColor object in my Type extension method, the UIColor constructor is referencing the encapsulating UIColor…
gangelo
  • 3,034
  • 4
  • 29
  • 43
0
votes
0 answers

Access swift enums in obj C

I have created an extension for UIColor in swift to add custom colors. The colors I have grouped together in their own namespace for which I have used enums with no cases. Here is the class: extension UIColor { public enum FirstGroup { …
iOSer
  • 235
  • 2
  • 14
0
votes
0 answers

Extension method on UIView that will be called upon init

I'm trying to write an extension method on UIView that will be called whenever a view is initialized, meaning that the method should be invoked without my having to call on it explicitly. Is there a way to accomplish this such as with key-value…
etayluz
  • 15,920
  • 23
  • 106
  • 151
0
votes
2 answers

iOS: return value from NSNotificationCenter in an extension (swift)

In my extension I put the control for the keyboard notification in this way protocol KeyboardSpaceProtocol { func addObservers() func removeObservers() func keyboardWillShow(notification:NSNotification) -> CGFloat } extension UIView:…
cyclingIsBetter
  • 17,447
  • 50
  • 156
  • 241