swift-extensions

Access static property of protocol extension

巧了我就是萌 提交于 2020-01-03 03:06:07
问题 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 get to work: protocol NibInstantiable: class { static var bundle: Bundle? { get } static var nibName: String { get } } extension NibInstantiable where Self: UIViewController { // static var nibName: String { // return "" // } static func

Swift 'open' keyword & overridable method/properties in extension?

寵の児 提交于 2020-01-01 02:51:46
问题 With introduction of open keyword in Swift 3.0 (What is the 'open' keyword in Swift?). Note: Limited to extensions on NSObject derived classes or @objc attributed method/properties. Code wich declared and used public ( class ) methods/properties in extension across modules/frameworks broke, as public is no longer means 'overridable' outside of defining module. Example: public extension UIManagedDocument { public class func primaryDocumentName() -> String { return "Document" } public class

Extension may not contain stored property but why is static allowed

橙三吉。 提交于 2019-12-28 02:55:09
问题 Extension cannot contain stored property, but why then can static stored property be defined within extension? I also didn't find any documentation mentioning that static property is allowed in extension. extension String { static let test = "Test" static var test2 = "Test2" } 回答1: Extensions cannot contain stored instance properties. Why? Because adding an instance property would change the size of instances of that type. What happens if one module adds an extension such that an Int is now 2

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

风格不统一 提交于 2019-12-25 09:09:42
问题 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,T) and shows compiler error extra argument in call . Here's a simple way to reproduce the error: extension Array { func smallestInt(first: Int, second: Int) -> Int { return min(first, second) // compiler error: "Extra argument in call" } } I get the same error when adding the same function to an extension of Dictionary , while the exact

how to modify UIButton's accessibilityLabel in an extension

浪尽此生 提交于 2019-12-25 00:25:44
问题 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 name coming from image assets I use accessibilityLabel itself. Some issues I faced: can't access accessibilityLabel within its getter. Because that would recursively look for accessibilityLabel . So I had to use another property for backing and

Swift 4: type(of: self) differs when using private/fileprivate

大兔子大兔子 提交于 2019-12-24 14:43:16
问题 I implemented an extension to NSObject to get the dynamic type of my objects: extension NSObject { var dynamic_type : String { get { return String(describing: type(of: self)) } } } This works perfectly for public classes. In a class called InitialState dynamic_type would be "InitialState" (this is what I want) But as soon as I change the class to private or fileprivate it is something like "(InitialState in _AF5C6D4A3B423A6F0735A7740F802E5A)" (the parenthesis are also returned) Why is this

Extend Swift Array to Filter Elements by Type

落花浮王杯 提交于 2019-12-24 00:47:17
问题 How can a swift array be extended to access members of a particular type? This is relevant if an array contains instances of multiple classes which inherit from the same superclass. Ideally it would enforce type checking appropriately. Some thoughts and things that don't quite work: Using the filter(_:) method works fine, but does enforce type safety. For example: protocol MyProtocol { } struct TypeA: MyProtocol { } struct TypeB: MyProtocol { } let myStructs:[MyProtocol] = [ TypeA(), TypeA(),

An extension hides a property that I want to access. Workarounds?

六月ゝ 毕业季﹏ 提交于 2019-12-24 00:43:55
问题 I am using two pods: DropDown and SwiftyUtils. DropDown adds in a UIView subclass called DropDown . The DropDown class defines its own width property. Instead of setting the frame , the client code has to set the width of the drop down menu using this property. It is defined like this: public var width: CGFloat? { didSet { setNeedsUpdateConstraints() } } SwiftyUtils on the other hand, added an extension to all UIView s. In the extension, there is a width property as well. This width property

How to use @objc protocol with optional and extensions at the same time?

本小妞迷上赌 提交于 2019-12-22 18:44:05
问题 This code does not compile and might sound stupid as it is, but i'll explain why it's so important! @objc protocol p { optional func f1() func f2() } extension p { func f1() { } func f2() { } } class foo: p { } Compiler says Type c does not conform to protocol 'p' and that's maybe because you can not use @objc optional and extensions at the same time (and does not make sence in this scenario either). But consider the following example: I want to set a selector on a non-optional method defined

Generic IBDesginables UIView extension

与世无争的帅哥 提交于 2019-12-22 00:36:32
问题 I would like to create generic extension for class to add some designables functionality to any UIView subclass and by this avoid adding functionality to all subclasses. So, would be nice to add extension for UIView which conforms to protocol SomeProtocol (It's empty because it is just a tag to mark classes which I want functionality to be added). Then just add that protocol in any UIView subclass where I want that functionality to be implemented like this: protocol SomeProtocol { //empty }