swift-extensions

Protocol extension in Swift 3 [duplicate]

。_饼干妹妹 提交于 2019-12-20 07:47:10
问题 This question already has an answer here : EXC_BAD_ACCESS Using IBInspectable (1 answer) Closed 2 years ago . I want to have a default property of UIImageView , which would be isFlipped . I am able to do it by subclassing UIImageView and adding one property isFlipped . But I want to user protocol and extensions for this , but it is crashing after sometime. Below is my code. How can I use it in right way? Thanks import Foundation import UIKit protocol FlipImage { var isFlipped: Bool { get set

How to create several cached UIColor

被刻印的时光 ゝ 提交于 2019-12-17 21:31:12
问题 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 colors. These colors will be as calibrated as possible. // These colors are cached. open class var black: UIColor { get } // 0.0 white open class var darkGray: UIColor { get } // 0.333 white [...] I've created an extension of UIColor, like so : import

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

南笙酒味 提交于 2019-12-17 20:03:43
问题 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 self.count == 0 { return 0 } else if self.first is Collection { // .Iterator.Element: Collection return self.reduce(0) { (res, elem) -> Int in res + (elem as! Collection).flatCount // ERROR } } else { return self.reduce(0) { (res,_) in res + 1 } } } } However,

Swift Extension: same extension function in two Modules

谁说胖子不能爱 提交于 2019-12-17 18:39:14
问题 Say I have a Framework called SwiftKit, which has a UIView's extension class method named someClassMethod and a property named someProperty within it: // SwiftKit public extension UIView { class func someClassMethod() { print("someClassMethod from Swift Kit") } var someProperty: Double { print("someProperty from Swift Kit") return 0 } } And I also have a Framework called SwiftFoundation, which also has a UIView's extension class method named someClassMethod and a property named someProperty

Idiomatic way to test Swift Optionals

只愿长相守 提交于 2019-12-13 16:23:40
问题 I'm NOT asking if these extensions are a good idea or not, it's just a thought experiment, I'm trying to learn from the practice. Agreeing with Christian Lattner, that methods are generally preferable, I thought I'd play with being able to express: someVariable.isNil and someVariable.notNil Implementing it, I found myself curious if one or the other of the following implementations was preferable to the other, and for what reasons? Would one be more efficient than the others. Would there be

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

孤人 提交于 2019-12-13 13:15:07
问题 (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 protocol, extension, and forcing IntegerType and FloatingPointType to adopt that. So I thought I'd just go the more redundant route and just extend IntegerType directly (and then repeat the code for FloatingPointType ). extension IntegerType { var

Extending existing protocol to conform to another protocol

核能气质少年 提交于 2019-12-12 17:02:04
问题 Hello my goal here is to extend (in swift 3) a protocol I have in a framework to conform to another Protocol protocol SomeProtocol {} protocol SomeOtherProtocol {} extension SomeOtherProtocol: SomeProtocol {} The problem here is that I get an error: "Extension of protocol 'SomeOtherProtocol' cannot have an inheritance clause" If this is not possible how do I achieve such thing or what's the "swift way"? 来源: https://stackoverflow.com/questions/44425503/extending-existing-protocol-to-conform-to

UICollectionview in a keyboard extension

[亡魂溺海] 提交于 2019-12-12 02:38:38
问题 I have tried to get a UICollectionview into my keyboard extension, which i made in swift. But i can´t get it to work. The keyboard just crashes on launch. Anyone know how to fix this? Or is it even possible to use a UICollectionview in a keyboard extension. class KeyboardViewController: UIInputViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource{ @IBOutlet var nextKeyboardButton: UIButton! @IBOutlet var testButton: UIButton! var collectionView: UICollectionView!

How can I extend dictionaries whose values are dictionaries themselves?

不羁岁月 提交于 2019-12-12 01:22:52
问题 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 subscript(k1: K1, k2: K2) -> V? { return self[k1]?[k2] } } I can not get this to work, though. Type bounds can not be non-protocol types; no protocol that Dictionary implements provides the methods I and types I need to refer to; getting access to the types of generics is

swift- Cannot convert value of type '[Int]' to expected argument type '[_]'

风格不统一 提交于 2019-12-11 14:32:26
问题 I'm trying to implement a zip function as an extension to Array, and I'm intending for it to be used like this: let myKeys = ["a","b","c"] let myVars = [1,2,3] myKeys.zip(myVars) // ["a":1,"b":2,"c":3] Here is my attempt: extension Array { public func zip<T,U>(vals: [U]) -> [T:U] { var dict: [T:U] = Dictionary() for (i,key) in self.enumerate() { if let k = key as? T { dict[k] = vals[i] } } return dict } } let myKeys = ["a","b","c"] let myVars = [1,2,3] myKeys.zip(myVars) // ERROR: Cannot