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

I have a confusion if any methods are declared in class extension are static or not

class Test: UIViewController{ func abc() { print("This is ABC") } } extension Test { func def(){ print("This is DEF") } } My question here is that what is the difference between both the methods declared? Is…
0
votes
1 answer

How can I reference the Self type in a `class func` in an extension for a class

So this is a little tricky, but I'll outline the main points: I have a class defined in an Objective-C header from a pre-compiled framework. We'll call BaseClass This class is meant to be subclassed BaseClass has a class function called…
A O
  • 5,516
  • 3
  • 33
  • 68
0
votes
2 answers

Dictionary extension in Swift is not recognized

I am trying to extend Dictionary with the following code: extension Dictionary where Key: ExpressibleByStringLiteral, Value: AnyObject { var jsonString: String? { if let dict = (self as AnyObject) as? Dictionary { …
bashan
  • 3,572
  • 6
  • 41
  • 58
0
votes
0 answers

Which one is best to give a shadowColor, Radius, Opacity and cornerRadius for a UIButton? button extension or IBDesignable?

i'm new to swift,i know little bit about Extension and IBdesignable. extensions will give effects at runtime and IBDesignable will give live rendering in storyboard without run the application.which is more effective? i want to know deep in this…
0
votes
1 answer

How to create a global function that affects all or specific UIButton instance types?

I want to create a global function that affects either all UIButton instances or only those of a certain UIButton type which would update the corner radius or border property. I'm familiar with UIAppearances however my client would like to have a…
Laurence Wingo
  • 3,912
  • 7
  • 33
  • 61
0
votes
1 answer

How can I use a class extension to override a protocol extension?

Let's say I have a color model: protocol Color { var value: String? { get } } class UnknownColor: Color { let value: String? = nil } class KnownColor: Color { let value: String? init(value: String? = nil) { self.value =…
G-P
  • 111
  • 4
0
votes
1 answer

how to modify UIButton's accessibilityLabel in an extension

I'm using an analytics tool which logs the accessibilityLabel of buttons. I'm trying to find a way to update the accessibilityLabel without changing my existing code. For normal buttons I use the titleLabel.text. For iconButtons which use their the…
mfaani
  • 33,269
  • 19
  • 164
  • 293
0
votes
1 answer

self vs "generic typet" T difference when ceating extension

I run into an interesting behaviour which I don't understand. Here is code that produces this behaviour: import UIKit protocol UIViewNibLoading { static var nibName: String { get } } extension UIView : UIViewNibLoading { static var…
Vojta
  • 810
  • 10
  • 16
0
votes
0 answers

Swift Operator Identity Check

Operator Identity Check Swift 4.1, Xcode 9.3 I know that the === operator is used in Swift to check the identity of an operand. I have a situation whereby I want to check the identity of my operand against that of an operator – I need to check if my…
Noah Wilder
  • 1,656
  • 20
  • 38
0
votes
1 answer

Extension available for use without compilation

I wanted to check if it is necessary to compile the code before using a just created extension. As a test, I created a sample extension extension String { func checkExtension() { } } When I did that, neither did I save the changes in…
Nitish
  • 13,845
  • 28
  • 135
  • 263
0
votes
1 answer

Swift: extension of enum UIModalTransitionStyle

How can I add a new case in enum UIModalTransitionStyle? Is that feasible using swift extension? Swift document about extension says that: Extensions add new functionality to an existing class, structure, enumeration, or protocol type. Does it…
Krunal
  • 77,632
  • 48
  • 245
  • 261
0
votes
2 answers

Cannot Access .size withAttributes: from String

I have a String extension: func iconName(identifier: String) -> String { return self.library()[identifier] } and I'm doing this inside a UIImage extension: let icon = String.iconName("myiconname") var iconSize = icon.size(withAttributes:…
tentmaking
  • 2,076
  • 4
  • 30
  • 53
0
votes
1 answer

UIColor swift extension w/ class access from Objective-C

How can I access my darkGray color from objective-c please? @objc extension UIColor { @objc public class Scheme1: NSObject { static var darkGray: UIColor! { return UIColor(red: 16.0/255.0, green: 16.0/255.0, blue:…
Chris Allinson
  • 1,837
  • 2
  • 26
  • 39
0
votes
1 answer

how to call UITextFieldDelegate in UIView

I am not able to call the delegate method of UITextField, when I click one textfield in my View. func textFieldShouldReturn(_ textField: UITextField) -> Bool any idea? import UIKit class CustomUIViewScreen: UIView { override func awakeFromNib()…
dicle
  • 1,122
  • 1
  • 12
  • 40
0
votes
1 answer

How to Add Another Level to an Extension (categorize like extensions)

I want to take a bunch of like functions/mutating functions inside an extension and store (categorize) them under one name. So let's say I have this: extension Int { mutating func addTen() { self = self + 10 } mutating func…
Noah Wilder
  • 1,656
  • 20
  • 38