Questions tagged [swift-extensions]

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

Apple Pre-release Docs

394 questions
4
votes
3 answers

Replacing Multiple Occurrences in Array - Swift 4.1

Replacing Multiple Occurrences in Array Swift 4.1, Xcode 9.3 I want to make an extension alike an extension I made for Array for String. String Extension: public extension String { /// /// Replaces multiple occurences of…
Noah Wilder
  • 1,656
  • 20
  • 38
4
votes
1 answer

Error while accessing members of UIColor extension

I want to move custom colors to an extension of UIColor: extension UIColor { static var nonSelectedTabColor: UIColor { return UIColor(white: 0.682, alpha: 1) // #AEAEAE } } But on trying to access it, its causing me an…
j.krissa
  • 223
  • 5
  • 21
4
votes
3 answers

How to override computed property of protocol extension in Swift

I want to implement a theming in a way, that a feature can add its needed colors to a Theme protocol, so that any actual theme implementation must provide colors for every feature. I also want to have the theme implementation and feature theme…
tosi
  • 787
  • 1
  • 7
  • 12
4
votes
2 answers

Extension for UIColor with custom colors it is real?

I have some custom colors for my application and now its saves like a dictionary, but I think this is no really good idea and I want to do extension for UIColor with a custom color. That may look like this var newColor = UIColor.MyColor // like…
Golovanov Dmytrii
  • 527
  • 1
  • 6
  • 20
4
votes
1 answer

If implementing a protocol method in an extension, do you need to specify the method in the protocol?

I like how Swift lets you define a method in a protocol, then via an extension of that protocol, implement that method. However, specifically for cases where you're defining the protocol and the extension in the same scope and with the same access…
Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286
4
votes
1 answer

What's the difference between == and : in swift extension where condition

I have a question in swift extension: protocol Racer { var speed: Double { get } } extension Sequence where Iterator.Element == Racer { func fastest() -> Iterator.Element? { return self.max(by: { (a: Iterator.Element, b:…
Klein Mioke
  • 1,261
  • 2
  • 14
  • 23
4
votes
4 answers

How to create several cached UIColor

I have custom colors within my code. I use them several times and I would like to have them allocated only once. The situation / problem If we get a look at UIColor headers we can see the following : [...] // Some convenience methods to create…
lchamp
  • 6,592
  • 2
  • 19
  • 27
4
votes
3 answers

Single extension for UITextView and UITextField in Swift

I want to create a single extension for both UITextField and UITextView and add a below method to it: func addDoneButtonOnKeyboardWith(selector : Selector) { let keyBoardToolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: 320,…
PGDev
  • 23,751
  • 6
  • 34
  • 88
4
votes
1 answer

How to constrain the type of Generator.Element in a Protocol extending SequenceType

Assume I'm writing a social media crawler that handle's multiple accounts (e.g. Facebook, Twitter, etc.) I define a few protocols for messages (Message has display-name and message body, Timestamped has a timestamp, Forwarded has the original…
Feenaboccles
  • 412
  • 3
  • 12
4
votes
3 answers

Issue in rotating UIImage - IOS/Swift

For rotating an UIImage I used an extension which is downloaded from the internet. There, rotating is working but there are some wired behaviours. This is the extension code import UIKit extension UIImage { public func…
Hanushka Suren
  • 723
  • 3
  • 10
  • 32
4
votes
1 answer

Swift extension on RawRepresentable has no accessible initializer

I'm trying to create an extension for my FieldIdentifiable protocol only where the enum that implements it has a RawValue of Int. The only problem is that the return FieldIdItem(rawValue: newValue) line keeps showing this error: 'Self.FieldIdItem'…
programmerdave
  • 1,088
  • 1
  • 9
  • 17
4
votes
1 answer

Swift: Are global functions bad practice?

I've read that global variables are bad. But, what about global functions? Say I add to my iPhone app project a file called Globals.swift containing func foo() { print("foo") }. Isn't that bad because it could conflict with other global functions of…
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
4
votes
3 answers

Trying to extend IntegerType (and FloatingPointType); Why can't all Int types be converted to NSTimeInterval

(This probably needs a better title...) I would like to have a set of accessors I can use in code to quickly express time durations. E.g: 42.seconds 3.14.minutes 0.5.hours 13.days This post illustrates that you can't just do it with a simple new…
Travis Griggs
  • 21,522
  • 19
  • 91
  • 167
4
votes
1 answer

Swift: When we made an extension to Int, what's the "self" within a property getter supposed to be?

It seems that it's a bug and resolved in Xcode 7 with Swift(2b3) To make convenient observation, let's post the code to playground, the result will be indicated by the comments. extension Int { var sq: Int { mutating get { …
Shaowen Su
  • 41
  • 4
4
votes
1 answer

UIButton Border Color on Selected State using IBInspectable

I am using @IBInspectable to set a border color on a UIButton using a Swift extension like this: extension UIButton { @IBInspectable var borderWidth: CGFloat { get { return layer.borderWidth } set { layer.borderWidth =…
Clifton Labrum
  • 13,053
  • 9
  • 65
  • 128